Why is the external “final” not fulfilled when the internal “trick” is thrown?

I wrote a simple program to test the theory that the finally block will always be executed no matter what. But what I see from the pgm below is that the control never enters an external final block.

I tried to make F5 as well as Ctrl-F5 in Visual Studio, and this is the same result.

Can someone explain why I see this behavior?

The output in the console window:

internal catch

internal finally

external catch

raw execution:

.. and then the application crashes

public class Program
{
    static void Main()
    {
        try
        {
            try
            {
                string s = null;
                s.ToString();
            }
            catch
            {
                Console.WriteLine("inner catch");
                throw;
            }
            finally
            {
                Console.WriteLine("inner finally");
            }

            return;
        }
        catch
        {
            Console.WriteLine("outer catch");
            throw;
        }
        finally
        {
            Console.WriteLine("outer finally");
        }
    }
}
+5
source share
2 answers

" " .

:

inner catch
inner finally
outer catch

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
   at SOConsole.Program.Main() in c:\Users\DaveShaw\Documents\Visual Studio 11\Pro
jects\SO\SOConsole\Program.cs:line 35
outer finally
Press any key to continue . . .

: Exception

, "", " ".

- , " ", . "", , , , , . .

+7

, , stdout.

( WriteLine), - . A finally : !

+4

All Articles