Multiple catch with java 7

Java 7 has introduced a new way of writing multiple catch blocks when we want to perform similar task in each catch block.

Consider below code snippet

  1. try {
  2.  
  3.     execute(); //exception might be thrown
  4.  
  5. } catch (IOException ex) {
  6.  
  7.     LOGGER.error(ex);
  8.  
  9. } catch (SQLException ex) {
  10.  
  11.     LOGGER.error(ex);
  12.  
  13. } catch (CustomException ex) {
  14.  
  15.     LOGGER.error(ex);
  16.  
  17. }
try {
 
    execute(); //exception might be thrown
 
} catch (IOException ex) {
 
    LOGGER.error(ex);
 
} catch (SQLException ex) {
 
    LOGGER.error(ex);
 
} catch (CustomException ex) {
 
    LOGGER.error(ex);
 
}


The method getting called in try block may throw one of the below 3 exceptions
1) IOException
2) SQLException
3) CustomException

We have written separate catch block for each of these exceptions

If we observe the code in all catch blocks, we are doing the same task which is logging the exception

This makes redundant code and Sonar will complain about duplicating code and it’s not good in any programming

So, Java 7 has provided a new feature to handle multiple catch blocks without duplicating the code

Check the below code snippet

  1. try {
  2.  
  3.     execute(); //exception might be thrown
  4.  
  5. } catch (IOException | SQLExceptionex | CustomException ex) {
  6.  
  7.     LOGGER.log(ex);
  8.  
  9. }
try {
 
    execute(); //exception might be thrown
 
} catch (IOException | SQLExceptionex | CustomException ex) {
 
    LOGGER.log(ex);
 
}


This improvement in java 7 is called multi-catch statement, in which each exception is separated by the vertical bar symbol ( | ) also called pipe symbol.

We can also combine multi-catch with regular catch blocks as below

  1. try {
  2.  
  3.   execute(); //exception might be thrown
  4.  
  5. } catch (IOException | SQLException ex) {
  6.  
  7.     LOGGER.error(ex);
  8.  
  9. } catch (CustomException ex) {
  10.  
  11.     // do something else with this custom exception
  12.     }
try {
 
  execute(); //exception might be thrown
 
} catch (IOException | SQLException ex) {
 
    LOGGER.error(ex);
 
} catch (CustomException ex) {
 
    // do something else with this custom exception
	}


In this example, we are handling IOException and SQLException in the same way, hence we have used single catch block for both using java 7 multi-catch feature

We have handled CustomException in different way, so we have written separate catch block for it

Note :
The exception variable declared within multi-catch block is implicitly final Therefore, we can not assign the variable to different value within the catch block.


Example, Following code snippet will give a compile error:

  1. catch (IOException | SQLException ex) {
  2.  
  3.     ex = new CustomException();
  4.  
  5. }
catch (IOException | SQLException ex) {
 
    ex = new CustomException();
 
}


Here, “ex” is final and can not be assigned anything further

Since we are trying to reassign “ex” inside catch block, it will throw compile time error

Note :
It is not allowed to specify two or more exceptions of a same hierarchy in the multi-catch statement.


Example , The following code snippet will give a compile error because the FileNotFoundException is a subtype of the IOException

  1. catch (FileNotFoundException | IOException ex) {
  2.  
  3.     LOGGER.error(ex);
  4.  
  5. }
catch (FileNotFoundException | IOException ex) {
 
    LOGGER.error(ex);
 
}


Below code also fails in compilation as Exception is the parent of IOException

  1. catch (IOException | Exception ex) {
  2.  
  3.     LOGGER.error(ex);
  4.  
  5. }
catch (IOException | Exception ex) {
 
    LOGGER.error(ex);
 
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on