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 shows.
Unwinding the stack by one level
static void Main()
Tester t = new Tester();
public void Run()
public void Func1()
Console.WriteLine("Entering try block...");
Console.WriteLine("Exiting try block...");
Console.WriteLine("Exception caught and handled!");
public void Func2()
throw new System.Exception();
Entering try block...
Exception caught and handled!
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:
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(). There is a catch block in Func1(), so its code is executed. 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" statement and the "Exit Func2" statement are not printed, try putting the code into a debugger and then stepping through it.
VISUAL STUDIO INTRODUCTION
C SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING