How the Call Stack Works

Examine the output of Example 16-2 carefully. You see the code enter Main(), Method1(), Method2(), and the try block. You never see it exit the try block, though it does exit Method2(), Method1(), and Main(). What happened?

When the exception is thrown, execution halts immediately and is handed to the catch block. It never returns to the original code path. It never gets to the line that prints the exit statement for the try block. The catch block handles the error, and then execution falls through to the code following the catch block.

If there is no exception handler at all, as we discussed, the stack is unwound, returning to the calling method in search of an exception handler. This unwinding continues until the Main() method is reached, and if no exception handler is found, the default (ugly) exception handler is invoked and the program terminates.

In this example, because there is a catch block, the stack does not need to unwind. The exception is handled, and the program can continue execution. Unwinding the stack becomes a bit clearer if you move the try/catch blocks up to Method1(), as Example 16-3 shows.

Example 16-3. The catch block has now moved from Method2 to Method1, so the program needs to unwind the stack by one level before finding the handler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example_16_3_ _ _ _Unwinding_the_Stack
{
    class Tester
    {
        public void Run()
        {
            Console.WriteLine("Entering Run...");
            Method1();
            Console.WriteLine("Exiting Run...");
        }

        public void Method1()
        {
            Console.WriteLine("Entering Method1...");
            try
            {
                Console.WriteLine("Entering try block...");
                Method2();
                Console.WriteLine("Exiting try block...");
            }
            catch
            {
                Console.WriteLine("Exception caught and handled!");
            }
            Console.WriteLine("Exiting Method1...");

        }

        public void Method2()
        {
            Console.WriteLine("Entering Method2...");
            throw new System.Exception();
            Console.WriteLine("Exiting Method2...");
        }
        static void Main()
        {
            Console.WriteLine("Entering Main...");
            Tester t = new Tester();
            t.Run();
            Console.WriteLine("Exiting Main...");
        }
    }
}

Now the output looks like this:

Entering Main...
Entering Run...
Entering Method1...
Entering try block...
Entering Method2...
Exception caught and handled!
Exiting Method1...
Exiting Run...
Exiting Main...

This time the exception is not handled in Method2(); it is handled in Method1(). When Method2() is called, it uses Console.WriteLine() to display its first milestone:

Entering Method2...

Then Method2() throws an exception and execution halts. The runtime looks for a handler in Method2(), but there isn’t one. Then the stack begins to unwind, and the runtime looks for a handler in the calling function: Method1(). There is a catch block in Method1(), so its code is executed. Execution then resumes immediately following the catch statement, printing the exit statement for Method1() and then for Main().

Notice that even though the exception is handled, you are now in Method1, and there is no automatic way to return to where you were in Method2.

If you’re not entirely sure why the Exiting try block statement and the Exiting Method2 statement are not printed, try putting the code into a debugger and then stepping through it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset