
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
380
|
Chapter 7: Exception Handling
try
{
//...
}
catch(Exception e)
{
Console.WriteLine("Error message == " + e.Message);
throw;
}
finally
{
try
{
//...
}
catch(Exception e)
{
Console.WriteLine(@"An unexpected error occurred in the finally block.
Error message == " + e.Message);
}
}
}
This block will prevent the original exception from being lost.
Discussion
Consider what would happen if an error were thrown from the inner finally block
contained in the
ThrowException method, as is instigated by the code shown in
Example 7-1.
Example 7-1. Throwing an error from the inner finally block of the
ThrowExceptionMethod
private void ThrowException( )
{
try
{
Console.WriteLine("In inner try");
int z2 = 9999999;
checked{z2 *= 999999999;}
}
catch(OverflowException ofe)
{
Console.WriteLine("An Overflow occurred. Error message == " +
ofe.Message);
throw;
}
catch(Exception e)
{
Console.WriteLine("Another type of error occurred. " +
"Error message == " + e.Message);
throw;
}
finally