O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required