Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing and Catching Exceptions

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

like image 931
tomato Avatar asked Aug 07 '12 20:08

tomato


4 Answers

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.

like image 77
smcg Avatar answered Oct 11 '22 14:10

smcg


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

like image 20
juergen d Avatar answered Oct 11 '22 14:10

juergen d


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.

like image 1
Nathan Hughes Avatar answered Oct 11 '22 15:10

Nathan Hughes



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.

like image 1
Jony Adamit Avatar answered Oct 11 '22 15:10

Jony Adamit