Catch, Rethrow and Filters – Why you should care?
A very common pattern in the usage of managed exception handling is that of catching an exception, inspecting it’s type and rethrowing it once you realize it was not the exception you wanted to handle. Below is such an example (and should be avoided in preference to another approach described further below in the writeup) that uses CustomBaseException as the base type of an exception hierarchy and CustomDontWantToCatchException as a type that derives from the base class that you wouldn’t want to catch: private static void Foo() { try { // Set some program state // Do some work that can possibly throw an exception } finally { // Reset the program state } } public static void Main() { try { Foo(); } catch (CustomBaseException ex) { if (ex is CustomDontWantToCatchException) throw; // Rethrow since we dont want to handle exceptions we aren’t interested in! else { // handle the exception } } } Managed exception handling comprises of two passes: 1.
See the rest here:
Catch, Rethrow and Filters – Why you should care?


