Exception thrown in catch and finally. CLR behavior vs. try-catch block

.net, c#, clr, exception, try-catch

Solution

The behavior you are seeing has nothing to do with a `finally` block throwing or not. You simply have an unhandled exception in your application, when that happens, all bets are off, including if `finally` blocks run or not:

From MSDN documentation:

Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement. Alternatively, you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.

If the finally block must run, then the solution is to do precisely what you've done in the second snippet: handle the unhandled exception.

Problem

I wrote simple C# console app: ``` class Mystery { static void Main(string[] args) { MakeMess(); } private static void MakeMess() { try { System.Console.WriteLine("try"); throw new Exception(); // let's invoke catch } catch(Exception) { System.Console.WriteLine("catch"); throw new Exception("A"); } finally { System.Console.WriteLine("finally"); throw new Exception("B"); } } } ``` The output given in console is: try catch Unhandled Exception: System.Exception: A at Mystery.Program.MakeMess() in ... It seems that CLR caught A and the finally block was not invoked at all. But when I surround call to MakeMess() with try-catch block: ``` static void Main(string[] args) { try { MakeMess(); } catch(Exception ex) { System.Console.WriteLine("Main caught " + ex.Message); } } ``` The output looks totally different: try catch finally Main caught B It seems that Exception that propagates from MakeMess() is different when Exception is strictly handled outside of method. What's the explanation of this behavior?

Original source

Related problems