Java catch multiple exceptions

In the previous tutorial, I have covered how to handle exceptions using try-catch blocks. In this guide, we will see how to handle multiple exceptions and how to write them in a correct order so that user gets a meaningful message for each type of exception.

Example
public class Test { public static void main(String args[]) { try{ int a[]=new int[5]; a[5]=30/0; } catch(ArithmeticException e){System.out.println("Test1 is completed");} catch(ArrayIndexOutOfBoundsException e){System.out.println("Test2 completed");} catch(Exception e){System.out.println("common Test completed");} System.out.println("rest of the code..."); } }


Output:
Test1 is completed rest of the code...

Example
class Test { public static void main(String args[]) { try{ int arr[]=new int[7]; arr[4]=30/0; System.out.println("Last Statement of try block"); } catch(ArithmeticException e){ System.out.println("You should not divide a number by zero"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Accessing array elements outside of the limit"); } catch(Exception e){ System.out.println("Some Other Exception"); } System.out.println("Out of the try-catch block"); } }


Output:
You should not divide a number by zero Out of the try-catch block
Now lets change the code a little bit and see the change in output:
class Test { public static void main(String args[]){ try{ int arr[]=new int[97]; arr[10]=14/25; System.out.println("Last Statement of try block"); } catch(ArithmeticException e){ System.out.println("You should not divide a number by zero"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Accessing array elements outside of the limit"); } catch(Exception e){ System.out.println("Some Other Exception"); } System.out.println("Out of the try-catch block"); } }


Output:
Last Statement of try block Out of the try-catch block

Rule: At a time only one Exception is occured and at a time only one catch block is executed.

Rule: All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception .

class Test { public static void main(String args[]) { try{ int a[]=new int[5]; a[5]=30/0; } catch(Exception e){System.out.println("common task completed");} catch(ArithmeticException e){System.out.println("task1 is completed");} catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");} System.out.println("rest of the code..."); } }


Output:
Compile-time error
What did we observe from the above two examples?
  1. It is clear that when an exception occurs, the specific catch block (that declares that exception) executes. This is why in first example first block executed and in second example second catch.

  2. Although I have not shown you above, but if an exception occurs in above code which is not Arithmetic and ArrayIndexOutOfBounds then the last generic catch handler would execute.

Lets change the code again and see the output:
class Test { public static void main(String args[]) { try{ int arr[]=new int[10]; arr[15]=14/2; System.out.println("Last Statement of try block"); } catch(Exception e){ System.out.println("Some Other Exception"); } catch(ArithmeticException e){ System.out.println("You should not divide a number by zero"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Accessing array elements outside of the limit"); } System.out.println("Out of the try-catch block"); } }


Output:
C:\new>javac Test.java Test.java:13: exception java.lang.ArithmeticException has already been caught catch(ArithmeticException e){ ^ Test.java:16: exception java.lang.ArrayIndexOutOfBoundsException has already bee n caught catch(ArrayIndexOutOfBoundsException e){ ^ 2 errors C:\new>java Test Last Statement of try block Out of the try-catch block C:\new>
Why we got this error?

This is because we placed the generic exception catch block at the first place which means that none of the catch blocks placed after this block is reachable. You should always place this block at the end of all other specific exception catch blocks.




Instagram