Tutorials

Java Exception Handling


Multiple Catch Block


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

 

What we mean to say is, our program use various concepts like array, file handling, database, etc. at the same time and each of them may throw exception due one reason or other. To catch the generic exception we simply use 'Exception' in the catch block parameter and it can catch every exceptions like ArrayIndexOutOfBound, FileNotFound, etc. But problem with this is we are not catching the specific exception.

 

For that reason we can have multiple catch block with a try block where each catch block would focus on catching a specific exception. See the example given below to understand how multiple catch works.

 

Example of multiple catch block in java:

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 of multiple catch block:

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 including ArithmeticException, ArrayIndexOutOfBoundException, FileNotFound, etc.
Thus the catch blocks that are declared after the first catch block will be unreachable code
 
The compiler will detect the presence of Exception in the first catch block and will report you - '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 : 111
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!