Multi Catch Blocks for Exceptions
If you are keeping an eye on the new features coming with JDK 7 then probably you could have also heard about multi catch blocks.
They are basically a new way to do things. The purpose of these blocks is to handle multiple exceptions. In pre JDK 7 world, if you wanted to catch two different exceptions but wanted to execute same code in the catch blocks then either you need two catch blocks with repeated code or a finally block.
The problem with finally block is that it gets executed even if no exception occurs which means we need to repeat code in catch blocks for printing stack trace or to re-throw exceptions.
But with JDK 7, if want to catch SQLException and IOException both with same code then I can write:
I have tried this new feature of JDK 7 with the release milestone M12. The multi catch feature is a part of project Coin which includes other features being introduced in JDK 7. Following is the code I had written:
Though I could not compile this code with Eclipse Helios, but I could successfully compile it using command line.
The variable ex is an instance of ArrayIndexOutofBoundsException because it is declared with it. I have written the following program to verify it:
They are basically a new way to do things. The purpose of these blocks is to handle multiple exceptions. In pre JDK 7 world, if you wanted to catch two different exceptions but wanted to execute same code in the catch blocks then either you need two catch blocks with repeated code or a finally block.
The problem with finally block is that it gets executed even if no exception occurs which means we need to repeat code in catch blocks for printing stack trace or to re-throw exceptions.
But with JDK 7, if want to catch SQLException and IOException both with same code then I can write:
try{ try-code } catch (SQLException | IOException) { catch-code }
I have tried this new feature of JDK 7 with the release milestone M12. The multi catch feature is a part of project Coin which includes other features being introduced in JDK 7. Following is the code I had written:
package com.example; public class Test{ public static void main (String args[]){ try { if (args[0].equals("null")) { throw (new NullPointerException()); } else { throw (new ArrayIndexOutOfBoundsException()); } } catch (NullPointerException | ArrayIndexOutOfBoundsException ex) { ex.getMessage(); } } }
Though I could not compile this code with Eclipse Helios, but I could successfully compile it using command line.
The variable ex is an instance of ArrayIndexOutofBoundsException because it is declared with it. I have written the following program to verify it:
public class Main{ public static void main(String[] args) { System.out.println("hello"); try { if (args[0].equals("null")) { throw (new NullPointerException()); } else { throw (new ArrayIndexOutOfBoundsException()); } } catch (NullPointerException | ArrayIndexOutOfBoundsException ex) { System.out.println(ex instanceof NullPointerException); System.out.println(ex instanceof ArrayIndexOutOfBoundsException); ex.printStackTrace(); } } } Ran it with the command: java -classpath c:\com\example Main And the output is: hello false true java.lang.ArrayIndexOutOfBoundsException: 0 at Main.main(Main.java:5)