Java catch multiple exceptions and rethrow exception

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn java catch multiple exceptions and rethrow exception.

java catch multiple exceptions and rethrow exception

Java catch multiple exceptions and rethrow exception

A java program can have ‘n’ number of catch blocks with single try block. But it is important to know that the order of catch block.

Also read – java rename file

Catch block is upgraded in java 7 to handle multiple exceptions in a single try catch block. Now let’s understand with an example,

public class JavaCatchMultiple 
{
   public static void main(String[] args) 
   {
      try
      { 
         int num[] = new int[5]; 
         num[5] = 30 / 0; 
      } 
      catch(ArithmeticException ae)
      {
         System.out.println("First catch");
      } 
      catch(ArrayIndexOutOfBoundsException aioe)
      {
         System.out.println("Second catch");
      } 
      catch(Exception e)
      {
         System.out.println("Default catch!!");
      } 
      System.out.println("Remaining code..."); 
   }
}

Output :

First catch
Remaining code…

We use multiple catch block when a java program execute various tasks at the instance of different exceptions.

NOTE :

  • “Try” block can have multiple “catch” block.
  • A single “try” block can have ‘n’ number of “catch” blocks.
  • If exception occurs, exception is passed to first “catch” block. If the exception matches in first “catch” block, exception is caught. Else exception is moved to next “catch” block. This will continue until the exception is caught.
  • Use multiple “catch” blocks for catching multiple exceptions and having similar code. Hence reducing code duplication.
  • At a time only one “catch” block is executed.
  • At a time only one exception is occurred.

Java rethrow exception

In rethrow statement, particular exception caught can be rethrown in the “catch” block.

Suppose in a java program if you are catching an exception and want that exception be known to the caller method, in that case rethrow exception is used.

NOTE: only checked exceptions can be rethrown.

Let’s see an example,

public class JavaRethrowDemo
{
   public static void main(String[] args) 
   {
      try 
      { 
         divideByZero(); 
      } 
      catch(ArithmeticException ae) 
      { 
         System.out.println("Rethrown exception in main() method"); 
         System.out.println(ae); 
      }
   }

   static void divideByZero() 
   { 
      int a,b,div; 
      try 
      { 
         a = 8 ; 
         b = 0 ; 
         div = a / b ; 
         System.out.println(a + "/" + b + " = " + div); 
      } 
      catch(ArithmeticException ae) 
      { 
         System.out.println("Exception caught!!! cannot divide by zero"); 
         throw ae; // rethrows exception 
      } 
   }
}

Output:

Exception caught!!! cannot divide by zero
Rethrown exception in main() method
java.lang.ArithmeticException: / by zero

Related Posts