Wednesday, 20 May 2015

Multi-catch statement in Java 7

There has always been criticism of checked exceptions in Java exception handling for being verbose and cluttering the code with try-catch blocks.
In Java 7 two features - Try-with-resources (Automatic resource management) and Multi-catch statement have been added to mitigate that problem to certain extent.
In this post we'll talk about multi-catch statement.

Handling more than one type of exception without Java 7 multi-catch

Before Java 7 multi-catch statement, if two or more exceptions were handled in the same way, we still had to write separate catch blocks for handling them.

catch(IOException exp){
    throw exp;
}catch(SQLException exp){
    throw exp;

It can be noted here that; though the catch blocks are doing the same thing, it is difficult to create a common catch block to eliminate duplicate code because variable exp has different types in both of the catch block.

Handling more than one type of exception with Java 7 multi-catch statement

With Java 7 and later it is possible to catch multiple exceptions in one catch block, which eliminates the duplicated code. Each exception type within the multi-catch statement is separated by Pipe symbol (|).

catch(IOException | SQLException exp){
    throw exp;

Note that If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter exp is final and therefore you cannot assign any values to it within the catch block.

According to JavaDoc - "Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers."

Multi-catch statement & Exception hierarchy

While joining multiple statement using | make sure that you go from subtype exception class to parent exception classes. Doing something like this will result in compiler error.

// This will give compiler error
catch(Exception | ArithmeticException | ArrayIndexOutOfBoundsException ex){

Here first exception itself is Exception which is up in the hierarchy than the other two exceptions. Thus it will give compile time error.

Also, specifying two or more exceptions of the same hierarchy in the multi-catch statement will result in compile time error.
As exp. - the following catch statement will give compile error because FileNotFoundException is a subtype of the IOException class.

catch (FileNotFoundException | IOException ex) {    

That's all for this topic Multi-catch statement. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. try-with-resources in Java 7
  2. Difference between Checked exception & Unchecked exception
  3. Difference between throw and throws
  4. final Vs finally Vs finalize
  5. finally block
  6. Multiple catch blocks
  7. Java Exception Handling interview questions

You may also like -

References :

1 comment: