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
- try {
- execute(); //exception might be thrown
- } catch (IOException ex) {
- LOGGER.error(ex);
- } catch (SQLException ex) {
- LOGGER.error(ex);
- } catch (CustomException ex) {
- LOGGER.error(ex);
- }
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
- try {
- execute(); //exception might be thrown
- } catch (IOException | SQLExceptionex | CustomException ex) {
- LOGGER.log(ex);
- }
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
- try {
- execute(); //exception might be thrown
- } catch (IOException | SQLException ex) {
- LOGGER.error(ex);
- } catch (CustomException ex) {
- // do something else with this custom exception
- }
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:
- catch (IOException | SQLException ex) {
- ex = new CustomException();
- }
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
- catch (FileNotFoundException | IOException ex) {
- LOGGER.error(ex);
- }
catch (FileNotFoundException | IOException ex) { LOGGER.error(ex); }
Below code also fails in compilation as Exception is the parent of IOException
- catch (IOException | Exception ex) {
- LOGGER.error(ex);
- }
catch (IOException | Exception ex) { LOGGER.error(ex); }