Tutorials

Java Exception Handling


Multiple Catch Block


In case code has possibility of throwing different kind of exception then programmer can use multiple catch blocks with a try block.

 

Example:

 

package exceptionHandling;

public class MultipleCatchBlock {

      public static void main(String[] args) {

            try{
                  int a = 20, b = 0;
                  int div = a/b;
           
                  int arr[] = {1,2,3,4,5};

                  System.out.println(arr[10]);
            }catch(ArithmeticException e){
                  System.out.println("Division by 0 not allowed");
            }catch(ArrayIndexOutOfBoundsException e){
                  System.out.println("Given index doesnot exist in array");
            }catch(Exception e){
                  System.out.println("Some exception occured");
            }
      }
}

OUTPUT:
Division by 0 not allowed

How multiple catch block works in java?

  • At a time, only one exception is caught and the respective catch block will get executed. For example, in the above code snippet, both int div =  a/b and arr[10] in println statement will result in exception. But since div = a/b will get executed first and will throw ArithmeticException. Thus catch block catch(ArithmeticException e) will get executed here.
  • Order of the catch block must be from most specific to most general one. 

For the second pointer let's consider an example to understand what it means - 'Order of the catch block must be from most specific to most general one'.

 

Example:

 

package exceptionHandling;

public class MultipleCatchBlock {

      public static void main(String[] args) {

            try{
                  int a = 20, b = 0;
                  int div = a/b;
           
                  int arr[] = {1,2,3,4,5};

                  System.out.println(arr[10]);
            }catch(Exception e){
                  System.out.println("Some exception occured");
            }catch(ArrayIndexOutOfBoundsException e){
                  System.out.println("Given index doesnot exist in array");
            }catch(ArithmeticException e){
                  System.out.println("Division by 0 not allowed");
            }
      }
}

OUTPUT:
Compile time error

What is the reason behind compile time error?

In above code snippet we have placed Exception (highlighted in yellow) in the very first catch block. 
Exception is itself capable of handling all type of exception like ArithmeticException, ArrayIndexOutOfBoundException, FileNotFound, etc.
Thus the catch blocks that are declared after the first catch block will be unreachable code
And the compiler will detect this and will report you the same that second and third catch block is unreachable code during compile-time.
 
In other words, Exception is a broader concept of which various other exception types is a part of. So any such exception will by default fall under Exception and the catch block containing it will get executed.


Please Share this page
Views : 43