Advertisement



< Prev
Next >



C# Nested try-catch Block





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

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




Important points to remember about nested try-catch structure





Advertisement




Example of nested try-catch block


In the upcoming program, a try-catch block is nested within another try-catch block. When the control of the program reaches the nested try-catch block, it throws an IndexOutOfRangeException exception, which is caught by the nested catch block because it has specified the matching exception.

//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)



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# Multiple Catch Blocks
C# Finally Block >



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