8

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 :)

0

6 Answers 6

12

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

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

1

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.

6
  • So you're saying that if I have an exception that I cannot handle inside of that method, I should throw it. But, if I can recover from it inside that same method, then that's when it is appropriate to catch inside the method. Also, should I leave the exception catching for the (methods, tests) that implement the underlying method that threw it?
    – tomato
    Aug 7, 2012 at 20:25
  • yes, you only catch what you can do something with. i didn't understand the last part, could you rephrase that? Aug 7, 2012 at 20:29
  • Sorry about that, let me try again. What I really meant to say is that when I use a method that throws an exception in some unit test or a main method, and let's say that I make the test throw the exception. What happens there? Does it just quit? Does it print error messages? What is the most appropriate thing to do in this situation?
    – tomato
    Aug 7, 2012 at 20:38
  • a main method will quit and print a stack trace. a unit test would probably fail and throw a stack trace (sometimes exceptions are handled differently than assert failures though)
    – smcg
    Aug 7, 2012 at 20:39
  • @Ejay: test frameworks have to handle failures, if a method throws an exception the test will record it and include it in its report, it won't stop the test. feel free to let exceptions be thrown from tests. Aug 7, 2012 at 20:50
1


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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.