Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try/Multi-Catch vs Single Catch

Tags:

java

try-catch

While adding a try/catch block in Eclipse, it gave me the option of "Surround with try/multi-catch" or "Surround with try/catch."

This is the try/multi-catch:

try {
    save.load(new FileInputStream(file.getAbsolutePath()));
}
catch (FileNotFoundException | IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

This is the single try/catch:

try {
    save.load(new FileInputStream(file.getAbsolutePath()));
}
catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

What are the benefits/repercussions of using one or the other? If I'm correct, the first example will execute the catch block when EITHER of the exceptions are thrown and produce the SAME CATCH, while the second example will throw a catch based on the exception while enabling separate catch blocks.

Is there anything else I should know about this? I've never used them before and don't know if they are worth using.

like image 748
Aaron Avatar asked Jul 14 '13 00:07

Aaron


2 Answers

tl;dr Mutlicatch handles things singlehandedly, multiple catch blocks are more flexible and nicer to operations. The two techniques can be combined.

If you have a try statement that can throw many different exception types, you'll want the multiple catch blocks. It's a bit more code but offers far greater flexibility.

For instance, if working with sockets, a SocketException may be caught with nothing more than a reconnect and/or an error message (as something as simple as an inadvertently disconnected cable may cause this)

If a null pointer exception(although it's unchecked) is caught, you're going to want to write to a log and make an emergency landing here, cleaning up what you can, and backtracking quite a bit codewise, possibly.

In addition, this can be subdivided even further, where different types of "common" exceptions may cause different actions to be taken(such as a connection lost vs name not resolved having different implications for the end-user on the first connection attempt) and different "heavy" exceptions being handled in different ways as well.

While you could have one (multiple exception type) catch block, it would either singlehandedly take similar action for all exceptions (presenting a null pointer exception to a user in the same way as a condition based on a cable being unplugged) or require if e instanceof FooException blocks that can decrease readability.

You can also combine the two, multicatching all "common" exceptions into a retry and nice message and all severe exceptions into a forced cleanup and shutdown

You don't want stacktraces for tripped cables, and you don't want to brush off missing objects.

like image 52
nanofarad Avatar answered Sep 19 '22 12:09

nanofarad


This a choice thing. You want to balance readability, portability, maintainability and also handling different exceptions differently.

So balance the use ... If all your catches use the same block of handling then use the first form, because that's just one code block and you aren't repeating yourself over and over again. The compiler can optimize things out a bit for you.

On the other hand use the second form if you are handling each exception differently.

This is somewhat of a broad question and the answer is dependant on your goals.

like image 23
Ahmed Masud Avatar answered Sep 21 '22 12:09

Ahmed Masud