Multiple Catch
using System;
/* One try block can have multiple catch blocks. Depending upon the type of exceptions raised,
corresponding catch blocks are executed.*/
public class MultipleCatch
{
static void Main(String[] args)
{
try
{
int[] a = new int[2];
string input = "test";
a[0] = Convert.ToInt32(input); // FormatException is raised here
Console.WriteLine("Access element three :" + a[3]); // IndexOutOfRangeException is raised here
}
catch (IndexOutOfRangeException e)
{
// IndexOutOfRangeException is caught here
Console.WriteLine("Exception thrown :" + e.Message);
}
catch (FormatException ex)
{
// FormatException is caught here
Console.WriteLine("Exception thrown:" + ex.Message);
}
Console.WriteLine("Out of the block");
Console.ReadLine();
}
}
Output:
Exception thrown:Input string was not in a correct format.
Out of the block