Subscribe Now!

Enter your email address:

Monday, May 23, 2011

Can we put multiple catch blocks in a single try statement

How to use multiple catch blocks in a single try statement

Yes. Multiple catch blocks may be put in a try block. See code example below, to see multiple catch blocks being used in C#. 

class ClassA
{
public static void Main()
{
int y = 0;
try
{
val = 100/y;
Console.WriteLine("Line not executed");
}
catch(DivideByZeroException ex)
{
Console.WriteLine("DivideByZeroException" );
}
catch(Exception ex)
{
Console.WritLine("Some Exception" );
}
finally
{
Console.WriteLine("This Finally Line gets executed always");
}
Console.WriteLine("Result is {0}",val);
}
}



Can we put multiple catch blocks in a single try statement

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...