Section 17.5. How the Call Stack Works

   

17.5 How the Call Stack Works

Examine the output of Example 17-2 carefully . You see the code enter Main( ), Func1( ), Func2( ), and the try block. You never see it exit the try block, though it does exit Func2( ), Func1( ), 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.

Because there is a catch block, the stack does not need to unwind. The exception is now handled; there are no more problems, and the program continues. This becomes a bit clearer if you move the try/catch blocks up to Func1( ), as shown in Example 17-3.

Example 17-3. Unwinding the stack by one level
 Option Strict On Imports System Namespace ExceptionHandling     Class Tester         Shared Sub Main( )             Console.WriteLine("Enter Main...")             Dim t As New Tester( )             t.Run( )             Console.WriteLine("Exit Main...")         End Sub 'Main         Public Sub Run( )             Console.WriteLine("Enter Run...")             Func1( )             Console.WriteLine("Exit Run...")         End Sub 'Run         Public Sub Func1( )             Console.WriteLine("Enter func1...")             Try                 Console.WriteLine("Entering Try block...")                 Func2( )                 Console.WriteLine("Exiting Try block...")             Catch                 Console.WriteLine("Exception caught and handled")             End Try             Console.WriteLine("Exit func1...")         End Sub 'Func1         Public Sub Func2( )             Console.WriteLine("Enter Func2...")             Throw New System.Exception( )             Console.WriteLine("Exit Func2...")         End Sub 'Func2 End Class 'Tester  End Namespace 'ExceptionHandling 
  Output:  Enter Main... Enter Run... Enter Func1... Entering try block... Enter Func2... Exception caught and handled! Exit Func1... Exit Run... Exit Main... 

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

 Enter Func2... 

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

If you're not entirely sure why the "Exiting Try Block" message and the "Exit Func2" message are not printed, try putting the code into a debugger and then stepping through it.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net