Multiple Catch Blocks In Java & Java 7 Multi Catch Syntax

By | January 16, 2018

The multiple catch blocks are examined in the order in which they appear. First the subclass catch block should come and then its super class catch block.


Multiple Catch Blocks In Java:

Multiple catch blocks are required:-

  • If a statement is prone to multiple exceptions.
  • If the try block contains multiple problematic statements.

The catch blocks are examined in the order in which they appear.

The first catch block that is capable of handling the exception is executed and remaining are ignored. Therefore it is recommended that the subclass catch block should come first and then its super class catch block.

Lets understand this with an example.

Here line 9 may throw two exceptions.

Refer Example1 and Output1: If the value of the variable div is zero(0) and the array element which is accessed is within the range, first catch block will work.

Refer Example2 and Output2: If invalid index is accessed, second catch block will work.

Example1:

 
public class Test
{
 public static void main(String args[])
 {
   int array[] = { 4, 6 ,30 }; 
   int div = 0;
 try
 {
  System.out.println(array[2]/div);
 }
 catch(ArithmeticException e)
 {
  System.out.println("Inside Catch 1");
  System.out.println(e);
 }
 catch(ArrayIndexOutOfBoundsException e)
 {
  System.out.println("Inside Catch 2");
  System.out.println(e);
 }
 }
}

Output1:

Inside Catch 1

java.lang.ArithmeticException: / by zero

Example2:

 
public class Test
{
 public static void main(String args[])
 {
   int array[] = { 4, 6 ,30 }; 
   int div = 0;
 try
 {
  System.out.println(array[-1]/div);
 }
 catch(ArithmeticException e)
 {
  System.out.println("Inside Catch 1");
  System.out.println(e);
 }
 catch(ArrayIndexOutOfBoundsException e)
 {
  System.out.println("Inside Catch 2");
  System.out.println(e);
 }
 }
}

Output2:

Inside Catch 2

java.lang.ArrayIndexOutOfBoundsException: -1

Now the following question arises.
Question: Why a programmer should take care of the order of catch statement?
Answer: In the above example, there is no need to take care of the order of catch because both (ArithmeticException  and ArrayIndexOutOfBoundsException ) the exceptions are of same level i.e. both are Runtime Exceptions and there isn’t any parent child relationship.

Problem arises when there is a parent child relationship among the exceptions. See the below example:

 
public class Test
{
 public static void main(String args[])
 {
 int num=20; 
 int div = 0;
 try
 {
 System.out.println(num/div);
 }
 catch(Exception e)
 {
 System.out.println("Inside Catch 1");
 System.out.println(e);
 }
 catch(ArithmeticException e)
 {
 System.out.println("Inside Catch 2");
 System.out.println(e);
 }
 }
}

Output:

Test.java:16: error: exception ArithmeticException has already been caught

catch(ArithmeticException e)

^ 1 error

Explanation:

As Exception is the superclass of ArithmeticException and in Java a super class can catch subclass exception. Therefore the ArithmeticException  is catched by the first catch block and the second catch block becomes unreachable and compile time error will be thrown.

Try executing the above program by changing the order of both the catch blocks.

Multi Catch Syntax:

Java 7 introduced a feature called Multi Catch Syntax. Where a single catch block can handle more than one type of exception. The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

Benefits Of Multi Catch Syntax:

  1. Byte-code generated by compiling a catch block that handles multiple exception types will be smaller than compiling many catch blocks that handle only one exception type each.
  2. Readability of the code increases.
  3. No need to write multiple catch blocks.

Example:

 
public class Test
{
 public static void main(String args[])
 {
   int array[] = { 4, 6 ,30 }; 
   int div = 0;
 try
 {
  System.out.println(array[-1]/div);
 }
 catch(ArithmeticException | ArrayIndexOutOfBoundsException e)
 {
  System.out.println(e);
 }
 }
}

Output:

java.lang.ArrayIndexOutOfBoundsException: -1

Rule:

It is not allowed to specify two or more exceptions of a same hierarchy in the multi-catch statement.

The following code snippet will give a compile error because IOException is a subclass of Exception:


import java.io.IOException;
public class Test{
  public static void main(String args[]){
   try{
     throw new IOException();
   }
   catch(IOException | Exception e){
     System.out.println(e);
   }
 }
}

Happy Learning 🙂

Leave a Reply