< Prev
Next >



Multiple Catch Blocks





So far, we have only seen one catch block associated with a try block. But it is also possible to have multiple catch blocks associated with one try block.




Why multiply blocks are useful?


Exception hierarchy contains many superclass exceptions and their subtypes/subclasses exceptions. Multiple catch blocks are used when we have to catch a specific type of exception, which is subclass of a general exception class. Let's see the use of multiple catch blocks with an example.




Multiple Catch Block Example


class MultipleCatch
{
public static void main(String... ar)
{
try
{
	int arr[]= {1,2,3,4};
	System.out.println("Value ="+arr[5]);   //ArrayIndexOutOfBoundsException thrown, invalid index 5.
}
catch(ArrayStoreException exp)     //catch block to handle/catch ArrayStoreException
{
	System.out.println(exp);
}
catch(ArrayIndexOutOfBoundsException exp)   //catch block to handle/catch ArrayIndexOutOfBoundsException
{
	System.out.println("Exception Caught - "+ exp);
}
catch(Exception exp)   //catch block to handle/catch Exception
{
	System.out.println(exp);
}

}
}


Output is -:


Exception Caught - java.lang.ArrayIndexOutOfBoundsException: 5


In this code, we have three catch blocks associated with one try block. At the runtime, code in try block throws an exception of type ArrayIndexOutOfBoundsException, this exception type is matched with the declared exception type in every catch-block(matching starts with the first catch block).


The catch block that matches with type of exception thrown is executed, while the rest of catch blocks are skipped. Let's see how -




Placement of a catch block is very important


The catch-block declared with a specific exception class must always be placed above the catch-block declared with a less specific/general superclass exception class, otherwise, Java Compiler will throw a compile-time error.



Let's understand this by an example -
class MultipleCatch2
{
public static void main(String... ar)
{
try
{
	int arr[]= {1,2,3,4};
	System.out.println("Value ="+arr[5]);   //ArrayIndexOutOfBoundsException thrown, invalid index 5.
}
catch(Exception exp)   //catch block to handle/catch a more general superclass "Exception"
{
	System.out.println("Exception Caught - "+ exp);
}
catch(ArrayIndexOutOfBoundsException exp)   //catch block to handle/catch a very specific subclass
						//of Exception class, "ArrayIndexOfBoundsException"
{
	System.out.println(exp);
}

}
}


Output


MultipleCatch2.java:18: error: exception ArrayIndexOutOfBoundsException has already been caught
catch(ArrayIndexOutOfBoundsException exp)   //catch block to handle/catch Exception
^
1 error


If this program throws an exception of type ArrayIndexOutOfBoundsException, this exception will be always be caught by the first catch-block, because Exception class is a superclass of ArrayIndexOutOfBoundsException exception class.


Hence, the second catch-block declared with an exception type ArrayOutOfBoundsException will not be reachable or executed even when the exception thrown has a specific match to it declared type, this leads to compile-time error.


In order to correct this compile-time error, the catch-block declared with ArrayOutOfBoundsException class must be placed above the catch-block declared with a general type Exception class, proving the value of placement of a catch-block in the multiple catch-blocks scenario.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Finally Block
Throw Keyword >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2
/