Multi Catch Exceptions
In Java 7, multi catch exceptions are introduced. As shown in below highlighted code, catch block can catch FirstException as well as SecondException.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package ravi.tutorial.java.java7; public class MultiExceptionCatchExample { public static void main(String[] args) { try { someMethod(1); } catch (FirstException | SecondException e) { e.printStackTrace(); } } public static void someMethod(int i) throws FirstException, SecondException { if (i == 1) { throw new FirstException(); } else if (i == 2) { throw new FirstException(); } } } class FirstException extends Exception { } class SecondException extends Exception { } |