Monday, May 27, 2013

Handling Multiple Exceptions in Java 7

Not much introduction in this post, but as most of you know, that was how Java SE used to handle multiple exceptions:
catch (DataFormatException ex) {
     ex.printStackTrace();
     throw ex;
catch (FileNotFoundException ex) {
     ex.printStackTrace();
     throw ex;
}
Now the problem rises when you do not want to write much code specifically to each clause, maybe you just are printing the stack trace or logging the exception. With the Java SE 7, you can write less code and be more flexible, something like this:
catch (DataFormatException|FileNotFoundException ex) {
    ex.printStackTrace();
    throw ex;
}

No comments:

Post a Comment