Advertisement



< Prev
Next >



Multiple Catch Blocks





So far in our previous tutorials, we have only seen a single catch block associated with a try block, but it is also possible to specify multiple catch blocks associated with a single try block.




Multiple catch are useful in such situations:







Multiple Catch Block Example


//C# Multiple Catch Block Example

using System;
using System.IO;

class MultipleCatch
{
public static void Main(String[] stArr)
{
	try
	{
		//Defining an array
		int[] arr = {1,2,3,4};

		//Trying to access an invalid index 5, an IndexOutOfRangeException will be thrown
		Console.WriteLine("Value ="+arr[5]);   
	}
	catch(ArrayTypeMismatchException exp)     //catch block to handle/catch ArrayTypeMismatchException
	{
		Console.WriteLine("An ArrayTypeMismatchException is caught");
	}
	catch(IndexOutOfRangeException exp)   //catch block to handle/catch IndexOutOfRangeException
	{
		Console.WriteLine("An IndexOutOfRangeException is caught");
	}
	catch(Exception exp)   //A general catch block to handle/catch Exception
	{
		Console.WriteLine("An Exception is caught");
	}

}
}


Output is -:


An IndexOutOfRangeException is caught

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 IndexOutOfRangeException, 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 -


Advertisement




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 -
//C# When defining multiple catch blocks, 
//the placement of each catch block is very important


using System;

class MultipleCatch2
{
public static void Main(String[] stArr)
{
	try
	{
		//Defining an array
		int[] arr = {1,2,3,4};	

		//Trying to access an invalid index, 5, so, the IndexOutOfRangeException will be thrown
		Console.WriteLine("Value ="+arr[5]);   
	}
	//A general catch block to handle/catch all exceptions, with superclass "Exception"
	catch(Exception exp)   
	{
		Console.WriteLine("Exception Caught - "+ exp);
	}
	//A catch block to handle a specific subclass of Exception class, "IndexOfRangeException"
	catch(IndexOutOfRangeException exp)  			
	{
		Console.WriteLine(exp);
	}

}
}


Output


MultipleCatch2.cs(25,8): error CS0160: A previous catch clause already catches all exceptions of this or of a super type ('Exception')


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


Hence, the second catch-block declared with an exception type IndexOutOfRangeException 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 IndexOutOfRangeException 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.



A try-catch block can even be nested


Just like you can nest an if statement within another if statement, a try-catch block can also be nested within a try-catch block. Let us show you an example.

Important points to remember about nested try-catch structure


Let's understand this by an example -
//C# Nested try-catch block


using System;

class MultipleCatch2
{
public static void Main(String[] stArr)
{
	//Outer try block
	try 
	{
		int a = 100, b = 10;
		int total = a + b;
		//A nested try block
		try
		{
			//Defining an array
			int[] arr = {1,2,3,4};	

			//Trying to access an invalid index, 5, so, the IndexOutOfRangeException will be thrown
			Console.WriteLine("Value ="+arr[5]);   
		}
		//A nested catch block to handle a specific subclass of Exception class, "IndexOfRangeException"
		catch(IndexOutOfRangeException exp)   
		{
			Console.WriteLine("Exception Caught - "+ exp);
		}
	}
	//An outer catch block to handle all exceptions, with superclass "Exception"
	catch(IndexOutOfRangeException exp)  			
	{
		Console.WriteLine(exp);
	}

}
}


Output


Exception Caught - System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at MultipleCatch2.Main(String[] stArr)


The program above has try-catch block nested within an outer try-catch block, the program on execution when reaches the nested try-catch block, throws an IndexOutOfRangeException exception, which is caught by the nested catch block because it has specified the matching exception.


Question: But what if the exception was not caught by the nested catch block? In that case, the exception is thrown to the outer catch block, to see if it is declared with the matching exception to handle the exception. Let us show you an example.

//C# A nested 


using System;

class MultipleCatch2
{
public static void Main(String[] stArr)
{
	//Outer try block
	try 
	{
		int a = 100, b = 10;
		int total = a + b;
		//A nested try block
		try
		{
			//Defining an array
			int[] arr = {1,2,3,4};	

			//Trying to access an invalid index, 5, so, the IndexOutOfRangeException will be thrown
			Console.WriteLine("Value ="+arr[5]);   
		}
		//A nested catch block to handle a specific subclass of Exception class, "ArithmeticException"
		catch(ArithmeticException exp)   
		{
			Console.WriteLine("Exception caught in the nested catch block: - ");
			Console.WriteLine(exp);
		}
	}
	//An outer catch block to handle a specific subclass of Exception class, "IndexOutOfRangeException"
	catch(IndexOutOfRangeException exp)  			
	{
		Console.WriteLine("Exception caught in the outer catch block: ");
		Console.WriteLine(exp);
	}

}
}


Output


Exception caught in the outer catch block:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at MultipleCatch2.Main(String[] stArr)


The program on execution throws an IndexOutOfRangeException exception when it is being executed in the outer try block, therefore, only the associated outer catch block could handle exception, which it does because it is declared with a matching exception type.




Please share this article -





< Prev
Next >
< C# Try Catch
C# Nested Try Catch >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement