[Microsoft Techies Blog] Defining Multiple Catch Blocks in console application.

1 view
Skip to first unread message

Amit thakur

unread,
Aug 6, 2011, 12:20:21 AM8/6/11
to techie...@googlegroups.com
You can define multiple catch blocks in sequence to catch different types of exception. When an exception occurs, the CLR examines each catch block in the order that was defined to find a catch block that can handle the exception. Therefore, you must ensure that you order the catch blocks from the most specific type to the most generic type to ensure that the CLR executes the most specific catch block for any given exception.

Following example shown.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter first number");
int i = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second number");
int j = Convert.ToInt32(Console.ReadLine());
int k = i / j;

int[] ii = { 1, 2, 3, 4 };
Console.WriteLine(ii[6]);

}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine(ex);
}

Console.WriteLine("Next line");
Console.ReadLine();
}
}
}
Output is display below.


--
Posted By Amit thakur to Microsoft Techies Blog at 8/05/2011 09:20:00 PM
Reply all
Reply to author
Forward
0 new messages