7
votes

Hey StackOverflow Community,

Regarding throwing exceptions. Generally, when do I throw and exception, and when do I catch it?

Let' say I come across these situation where I have to quit because of some problem that occurred and I can't recover from it. Do I throw or do I catch?

I do this right now:

    try {
       // some code
    }
    catch (IOException e) {
       logger.info("Failed to do something, and cannot continue" + e.getMessage(), e);
       e.printStackTrace();
       throw e;
    }

Is this the right thing to do? Would it be more appropriate if I just threw the exception? Sorry, I'm a newbie at exceptions :)

6

6 Answers

10
votes

You generally catch an exception in a method when you want your program to continue running. You throw an exception when you want a higher level method that is calling that method to handle the exception instead. For example, you might throw it all the way back to your Main method, which has a try..catch block (likely with different catch blocks for different exceptions) encapsulating all your method calls, and exceptions can be handled there (for example by ending the program).

Remember that throwing an exception will end the method immediately. This affects the flow of your code. If you might have an exception in the middle of the method, and the code below it can't run if that exception happened, then you would need to either wrap the whole section in a try/catch block or throw an exception.

A general word of advice - printStackTrace() is bad. You can create better error output yourself (and you can include the stack trace as well with your output). Even better, use logging.

I recommend reading this introduction to exceptions and this article which covers good and bad exception patterns.

1
votes

If a fatal exception occurs catch the exception and end your program nicely. Rethrowing without catching will just kill your program.

1
votes

If you know the exception is not something you can handle, let the exception go without catching it in the first place. Have one exception handler that catches everything and logs it to a file along with the stacktrace. For instance, if your program is running from the command line you can catch everything in the main method and log it there.

1
votes


You catch an exception when you have something to do with it. In your case - Write to the log, display a message to the user and quit in an orderly fashion.
You throw an exception when there is nothing else you can do about it when it happens.

I recommend you to use Microsoft's enterprise library exception handling application block. It will help you deal with your exceptions is a way that you would be able to control the flow and make changes configurationally.

0
votes

After catching i would log the incident and then do what you need in order to shutdown tidily and then call System.exit but i wouldn't throw that exception again.

0
votes

It is bad practice to have your program terminate due to an unhandled exception. If you catch an exception that is fatal and unrecoverable do the following:

  1. log it
  2. perform any necissary cleanup
  3. terminate the program