
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Assuring Exceptions Are Not Lost When Using Finally Blocks
|
379
Other developers reading your code may find it more natural to read the multiple
catch classes rather than the single catch clause with a decision structure inside of it.
There is one case in which you might consider using the single
catch clause with the
decision structure: when large amounts of code would have to be duplicated in each
catch clause and there is no way to put the duplicated code in a finally clause after
the
try-catch block. Even this scenario offers the alternative of structuring the code
in a nested
try-catch fashion like this:
try
{
try
{
...code here...
}
catch
{
...put the code that is common to handling all exceptions here
throw;
}
}
catch (ExceptionTypeA a)
{
... do specific exception A handling here
}
catch (ExceptionTypeB b)
{
... do specific exception B handling here
}
See Also
See the “Error Raising and Handling Guidelines” topic in the MSDN documentation.
7.5 Assuring Exceptions Are Not Lost When Using
Finally Blocks
Problem
You have multiple nested try-catch, try-finally, and try-catch-finally blocks. If a
catch block attempts to throw an exception, it is possible that the thrown exception
will get discarded and that a new and unexpected exception will be caught by an
outer exception handler. You want to ...