Are you catching exceptions that you want to then filter more carefully, so you can then change your mind, decide not to handle them and so rethrow them?
If you want to be really careful about this, that's not really a good idea. It's better to never catch the exception in the first place. The reason is that a given try/catch
handler should not take the decision to run nested finally
blocks for exceptions that it is not expecting to see. For example, if there is a NullReferenceException
, it's probably a very bad idea to continue executing any code as it will probably cause another such exception to be thrown. And finally
blocks are just code, like any other code. As soon as an exception is caught for the first time, any finally
blocks on the stack beneath the try/catch
will be executed, by which time it's too late - another exception may be generated, and this means that the original exception is lost.
This implies (in C#) that you have to careful write out a separate catch
handler for all the exception types you want to catch. It also implies that you can only filter by exception type. This is sometimes very hard advice to follow.
It should be possible to filter exceptions in other ways, but in C# it is not. However, it is possible in VB.NET, and the BCL itself takes advantage of this by having a small amount of code written in VB.NET so it can filter exceptions in a more convenient way.
Here's a detailed explanation, with a VB.NET code sample, from the CLR team blog.
And here's my two cents.