In the .NET Framework, when an exception occurs, the system will determine what if anything is going to catch that exception before any finally
blocks execute. Depending upon various application settings, an attempt to throw an exception which will not be caught may kill the application instantly, without giving any finally
blocks (or anything else) a chance to run.
If one wraps the Main
method, as well as each thread, in
try
{
...
}
catch
{
throw;
}
then any exception which is thrown within the try
block will get caught. Even though it will be immediately re-thrown, any nested finally
blocks will execute before the catch
. There are some cases where this is desirable behavior; there are other cases where one may wish to e.g. perform some special logging if an exception isn't going to be caught (in some cases, the information one wishes to log may be destroyed if the finally
blocks get a chance to run first). Within C#, there isn't any way to vary one's actions based upon whether an exception is going to be caught, but in VB.NET there are some ways via which that can be done; a VB.NET assembly which makes calls to C# code can give that code a way of knowing whether any exceptions thrown by an inner method will propagate out to the vb.net wrapper without being caught.