Tuesday, March 11, 2008

Processing specific exception in C#

It is necessary to process a specific exception type and re-throw the exception next.

This code will process a specific exception Exception1, but section finally will be processed in any case:

            try
{
throw exp;
}
catch (Exception1)
{
Console.WriteLine("catched Exception1 ");
throw;
}
finally
{
Console.WriteLine("finally");
}
Correct code to process a specific exception should be next:
try
{
throw exp;
}
catch (Exception1)
{
Console.WriteLine("catched Exception1 ");
throw;
}
catch (Exception e)
{
Console.WriteLine("catched not Exception1");
}
See console output of these samples:
consoleOutput 


No comments: