There are some semantic differences between catching and rebuilding an exception rather than catching it. Exceptional filters are therefore very useful, as they allow one, for example. "Catch Ex as an exception when IsNiceException (Ex)." Unfortunately, the only way to use them in a C # program is to use a DLL to wrap the necessary functionality (the DLL itself can be written in vb or in some other language). A typical template might be something like:
TryWhenCatchFinally (
() => {trycode;},
(Exception ex) => {codeWhichReturnsTrueForExceptionsWorthCatching;},
(Exception ex) => {codeToHandleException;},
(ExceptionStatus status, Exception ex) => {finallyCode;});
The ExceptionStatus parameter for the "final" code will be an enumeration that states: (1) there was no exception, (2) an exception occurred, but was handled, (3) an exception occurred and was handled, but another exception was (4) an exception occurred, but CodeWhichReturnsTrueForExceptionsWorthCatching returns false; (5) an exception occurred that was not handled in trycode and not handled by this block, but trycode ended in any case (a rare situation, but there are ways in which this can happen). The Ex parameter is null in the first case and contains the corresponding exception in other cases - potentially useful information if an exception occurs when processing the finally block (suppressing the exception that occurs in the finally block may be bad, but if the exception has not been previously logged or is not lost before the exclusion of the new exception occurs, all data from the earlier exception will be completely lost; if the same condition that caused the earlier exception caused a later, earlier lawsuit The communication could have more useful information about what happened wrong).
By the way, a few notes with this template:
- Code that decides whether to catch an exception will be executed before nested finally blocks are executed; it can capture useful data for logging (the fact that, finally, the blocks were not started, can make it possible to write information that will be destroyed by the nested finally blocks), but the actual cleaning should usually be done after finally running blocks.
- Currently, it seems that exceptions that might get away from filters are suppressed, but I'm not sure if the behavior is guaranteed. Operations that may leak exceptions should probably not be performed in filters.
- If the "trycode" contains a try-finally block that is nested in a try-catch block, the exception that occurs in the "try" part of this try-finally block is not handled by TryCatchWhenFamily and no nested area, but it is handled by an external block, and the processing of the internal block try-finally throws an exception that handles the internal try-catch block; an exception that the external block decided to catch may disappear without even being caught. If the TryWhenCatchFinally method is encoded to detect this condition, it can report this finally code block (the finally block may or may not want to do anything with the condition, but it probably should at least be registered).
source share