A try block could be followed by one or more catch blocks. Each catch block is meant for handling specific exception. If we would like to handle all possible exceptions in the same way then we could go for only one catch block by catching Exception/Throwable.
At a time, only one exception occurs in a try block and only one catch block executes.
Example 01 – try with multiple catch blocks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception07.multicatchblock; //try with multiple catch blocks public class Example01MultipleCatchBlocks { public static void main(String[] args) { System.out.println("main begin"); try { int a[] = new int[5]; a[10] = 5 / 0; // Here try followed by 2 catch blocks } catch (ArithmeticException e) { // this catch is to handle ArithmeticException System.out.println("ArithmeticException raised"); } catch (ArrayIndexOutOfBoundsException e) { // this catch is to handle ArrayIndexOutOfBoundsException System.out.println("ArrayIndexOutOfBoundsException raised"); } System.out.println("main end"); } } |
Output :
main begin
ArithmeticException raised
main end
In the above example, one try block is followed by multiple catch blocks. Here each catch block is used to deal with one specific exception.
Example 02 – try with multiple catch blocks – catch unknown exception
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 |
package com.vidvaan.corejava.exception07.multicatchblock; //try with multiple catch blocks - catch unknown exceptions public class Example02ToDealWithUnknownException { public static void main(String[] args) { System.out.println("main begin"); try { int a[] = null; a[10] = 5; } catch (ArithmeticException e) { System.out.println("ArithmeticException raised"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException raised"); } catch (Exception e) { // this catch is to handle unknown exceptions other than ArithmeticException, // ArrayIndexOutOfBoundsException System.out.println("Unknown Exception raised"); System.out.println(e.toString()); } System.out.println("main end"); } } |
Output :
main begin
Unknown Exception raised
java.lang.NullPointerException: Cannot store to int array because “a” is null
main end
In the above example, we don’t have catch block to deal with NullPointerException. In general we will define last catch block to catch any kind of exception to deal with unknown exception.
Example 03 – try with multiple catch blocks – catch blocks order
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 |
package com.vidvaan.corejava.exception07.multicatchblock; //try with multiple catch blocks - catch blocks order public class Example03MultipleCatchBlocksOrder { public static void main(String[] args) { System.out.println("main begin"); try { int a[] = new int[5]; a[10] = 5 / 0; } catch (Exception e) { System.out.println("Unknown Exception raised"); System.out.println(e.toString()); // Compilation Error : Unreachable catch block for ArithmeticException. It is // already handled by the catch block for Exception } catch (ArithmeticException e) { System.out.println("ArithmeticException raised"); // Compilation Error : Unreachable catch block for // ArrayIndexOutOfBoundsException. It is already handled by the catch block for // Exception } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException raised"); } System.out.println("main end"); } } |
Compilation Error : Unreachable catch block for XXXException. It is already handled by the catch block for Exception
In the above example, catch blocks are defined in order from generic to specific exception. It leads to Compilation error. So catch blocks order should always defined from specific to general exception(subclass to super class exception).
Example 04 – try with multiple catch blocks – one catch block catches multiple exceptions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.vidvaan.corejava.exception07.multicatchblock; //try with multiple catch blocks - one catch block catches multiple exceptions. public class Example04OneCatchMultipleExceptions { public static void main(String[] args) { System.out.println("main begin"); try { int a[] = new int[5]; a[10] = 5 / 0; } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { // It can able to catch ArithmeticException or ArrayIndexOutOfBoundsException System.out.println("ArithmeticException | ArrayIndexOutOfBoundsException handles in same way"); } catch (Exception e) { System.out.println("Unknown Exception raised, this could be handled in different way"); System.out.println(e.toString()); } System.out.println("main end"); } } |
Output :
main begin
ArithmeticException | ArrayIndexOutOfBoundsException handles in same way
main end
If we would like to handle couple of exceptions in same way then we could catch them in one catch block with ‘|’ operator between them. This feature is available from Java 1.7 version onwards.
If a
catch
block handles more than one exception type, then thecatch
parameter is implicitlyfinal