FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

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.

Catching multiple exceptions

Lets take an example to understand how to handle multiple exceptions.

class Example{
   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");
   }
}

Catch block

A catch block is where you handle the exceptions, this block must follow the try block. A single try block can have several catch blocks associated with it. You can catch different exceptions in different catch blocks. When an exception occurs in try block, the corresponding catch block that handles that particular exception executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.

Output:

You should not divide a number by zero
Out of the try-catch block

In the above example, the first catch block got executed because the code we have written in try block throws ArithmeticException (because we divided the number by zero).

Now lets change the code a little bit and see the change in output:

class Example{
   public static void main(String args[]){
      try{
         int arr[]=new int[7];
         arr[10]=10/5;
         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

Accessing array elements outside of the limit
Out of the try-catch block

In this case, the second catch block got executed because the code throws ArrayIndexOutOfBoundsException. We are trying to access the 11th element of array in above program but the array size is only 7.

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 Example{
   public static void main(String args[]){
      try{
         int arr[]=new int[7];
         arr[10]=10/5;
         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");
   }
}
Compile time error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception Unreachable catch block for ArrayIndexOutOfBoundsException. It is already handled by the catch block for Exception at Example.main(Example1.java:11)

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.







Leave Comment