Catching the Exception Object Farther up the Function Call Chain

   


As mentioned previously, an exception object travels up the function call chain until it finds a matching catch block. Consequently, it is possible to catch an exception object thrown in one function (like Method2) in a different function farther up the calling chain (like Method1). This is demonstrated in Listing 19.3, where the try-catch blocks of Method2 in Listing 19.2 have been moved to Method1. So, instead of enclosing the divide by zero statement as in the previous listing, the try block in lines 19 24 now encloses the call to Method2 in line 22.

Listing 19.3 CaughtInOtherFunction.cs
01: using System; 02: 03: class MyClass 04: { 05:     public static void Main() 06:     { 07:         Console.WriteLine("Entering MyClass.Main"); 08:         YourClass yourObject = new YourClass(); 09:         yourObject.Method1(); 10:         Console.WriteLine("Leaving MyClass.Main"); 11:     } 12: } 13: 14: class YourClass 15: { 16:     public void Method1() 17:     { 18:         Console.WriteLine("Entering YourClass.Method1"); 19:         try 20:         { 21:             Console.WriteLine("Entering the try block of Method1"); 22:             Method2(); 23:             Console.WriteLine("Leaving the try block of Method1"); 24:         } 25:         catch(DivideByZeroException exObj) 26:         { 27:             Console.WriteLine("Entering catch block of Method1"); 28:             Console.WriteLine("Exception: " + exObj.Message); 29:             Console.WriteLine("Exception was generated in Method2"); 30:             Console.WriteLine("Leaving catch block of Method1"); 31:         } 32:         Console.WriteLine("Leaving YourClass.Method1"); 33:     } 34: 35:     public void Method2() 36:     { 37:         Console.WriteLine("Entering YourClass.Method2"); 38:         int myInt = 0; 39:         int yourInt; 40:          //Dividing by zero 41:         yourInt = 10 / myInt; 42:         Console.WriteLine("Leaving YourClass.Method2"); 43:     } 44: } Entering MyClass.Main Entering YourClass.Method1 Entering the try block of Method1 Entering YourClass.Method2 Entering catch block of Method1 Exception: Attempted to divide by zero. Exception was generated in Method2 Leaving catch block of Method1 Leaving YourClass.Method1 Leaving MyClass.Main 

When the divide-by-zero-exception is thrown in line 41, no exception handler is found, so the normal execution is immediately stopped, exactly as in Listing 19.1 (line 42 is not executed). However, as opposed to Listing 19.1, the exception object is brought to line 22 from where Method2 was called. Because a try block encloses line 22 and the associated catch block matches the thrown exception object, this catch block now handles the exception. Notice how the execution flow returns to normal after the catch block has been executed, allowing line 32 and line 10 to be executed.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

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