C# Primer Plus
Authors: Michelsen K.
Published year: 2000
Pages: 222-223/286
Buy this book on amazon.com >>
   


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.


   
   


Multiple catch Blocks

If a try block contains code that can throw more than one type of exception object, (for example, DivideByZeroException as well as IndexOutOfRangeException ), you can specify more than one catch block relating back to the same try block (as shown in Syntax Box 19.1). When an exception object is looking for a matching catch block, the possible catch blocks are inspected in the sequence in which they appear in the source code. The first matching catch block will handle the exception. After this matching catch block has been executed, all other related catch blocks are ignored, and the execution commences after the end of the last catch block.

Note

graphics/common.gif

When multiple catch blocks are applied, the exception subclasses specified for the catch block parameters must precede any of their base classes in the sequence of catch blocks. Otherwise, these subclass catch blocks become unreachable, which is invalid in C#. For example, the second catch block in the following lines of code contains the parameter exception class System.DivideByZeroException , which is a subclass of System.Exception —the parameter class of the first catch block. As a result, any exception that is matched by System.DivideByZeroException is also matched by System.Exception . Consequently, the flow of execution will never reach the second catch block making the code invalid.


graphics/19infig02.gif



   
C# Primer Plus
Authors: Michelsen K.
Published year: 2000
Pages: 222-223/286
Buy this book on amazon.com >>

Similar books on Amazon