Multiple catch block

In Exception-Handling-try-catch article, we have seen how to handle single exception using try/catch block

Now in this article, we will see how to handle multiple exceptions

Whenever we handle multiple exceptions, order of catch block is very important.

Let’s understand with an example

  1. class MultipleExceptionHandling{
  2.    public static void main(String args[]){
  3.       try{
  4.       System.out.println("Begin: try block");
  5.          String name="abc";
  6.          int nameLength = name.length();
  7.          int res= 10/0;
  8.          
  9.          System.out.println("End: try block");
  10.       }
  11.       catch(ArithmeticException e){
  12.          System.out.println("Division is not allowed with zero denominator");
  13.       }
  14.       catch(NullPointerException e){
  15.          System.out.println("Name should not be Null");
  16.       }
  17.       catch(Exception e){
  18.          System.out.println("General Exception");
  19.       }
  20.       System.out.println("End: try-catch block");
  21.    }
  22. }
class MultipleExceptionHandling{
   public static void main(String args[]){
      try{
	  System.out.println("Begin: try block");
         String name="abc";
         int nameLength = name.length();
		 int res= 10/0;
		 
         System.out.println("End: try block");
      }
      catch(ArithmeticException e){
         System.out.println("Division is not allowed with zero denominator");
      }
      catch(NullPointerException e){
         System.out.println("Name should not be Null");
      }
      catch(Exception e){
         System.out.println("General Exception");
      }
      System.out.println("End: try-catch block");
   }
}


In the above example, We have handled multiple exceptions using multiple catch blocks.

We can write as many catch blocks as required after try block to handle multiple exception

Considering the above example, the first catch block got executed because the code in try block throws ArithmeticException as we divided the number by zero.

Note :
Whenever exception occurs in try block code, further lines of try block will not be executed and control goes to corresponding catch block and then continues further execution.


Let’s make small change to the above code

  1. class MultipleExceptionHandling{
  2.    public static void main(String args[]){
  3.       try{
  4.       System.out.println("Begin: try block");
  5.          String name=null;
  6.          int nameLength = name.length();
  7.          int res= 10/5;
  8.          
  9.          System.out.println("End: try block");
  10.       }
  11.       catch(ArithmeticException e){
  12.          System.out.println("Division is not allowed with zero denominator");
  13.       }
  14.       catch(NullPointerException e){
  15.          System.out.println("Name should not be Null");
  16.       }
  17.       catch(Exception e){
  18.          System.out.println("General Exception");
  19.       }
  20.       System.out.println("End: try-catch block");
  21.    }
  22. }
class MultipleExceptionHandling{
   public static void main(String args[]){
      try{
	  System.out.println("Begin: try block");
         String name=null;
         int nameLength = name.length();
		 int res= 10/5;
		 
         System.out.println("End: try block");
      }
      catch(ArithmeticException e){
         System.out.println("Division is not allowed with zero denominator");
      }
      catch(NullPointerException e){
         System.out.println("Name should not be Null");
      }
      catch(Exception e){
         System.out.println("General Exception");
      }
      System.out.println("End: try-catch block");
   }
}


In this case, the second catch block got executed because the code throws NullPointerException as String name is NULL and we are trying to access length of NULL string.

Whenever we write a code in try block which can cause multiple exceptions then its better to handle those exceptions with separate catch blocks.

Generic catch block is to handle all other exceptions apart from what we handle in specific catch blocks.

In above example, Generic catch block is the 3rd catch block which handles all other exceptions apart from NullPointerException and ArithmeticException

Note :
Its very important to note that, exception order should be followed in such a way that All specific exception should be handled first and then more generic exception should be handled otherwise we will get compile time error


Example :

  1. class MultipleExceptionHandling{
  2.    public static void main(String args[]){
  3.       try{
  4.       System.out.println("Begin: try block");
  5.          String name=null;
  6.          int nameLength = name.length();
  7.          int res= 10/5;
  8.          
  9.          System.out.println("End: try block");
  10.       }
  11.        catch(Exception e){
  12.          System.out.println("General Exception");
  13.       }
  14.       catch(ArithmeticException e){
  15.          System.out.println("Division is not allowed with zero denominator");
  16.       }
  17.       catch(NullPointerException e){
  18.          System.out.println("Name should not be Null");
  19.       }
  20.      
  21.       System.out.println("End: try-catch block");
  22.    }
  23. }
class MultipleExceptionHandling{
   public static void main(String args[]){
      try{
	  System.out.println("Begin: try block");
         String name=null;
         int nameLength = name.length();
		 int res= 10/5;
		 
         System.out.println("End: try block");
      }
	   catch(Exception e){
         System.out.println("General Exception");
      }
      catch(ArithmeticException e){
         System.out.println("Division is not allowed with zero denominator");
      }
      catch(NullPointerException e){
         System.out.println("Name should not be Null");
      }
     
      System.out.println("End: try-catch block");
   }
}


The above program will not be compiled, instead it gives below error

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.

Note :
Its important to write multiple catch blocks for one try block if there is a possibility of multiple exceptions in that try block Its very important to keep more specific exceptions first and then keep generic exceptions at the end

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on