Life without try-catch-finally

   


Life without try-catch-finally

Before we look at examples that implement the try-catch-finally trio introduced in the previous section, let's see what happens when an exception remains uncaught in your code. Listing 19.1 demonstrates this scenario.

Note

graphics/common.gif

To see the same output as that shown in the sample output after Listing 19.1, you must compile the listing with the debug compiler switch on as follows:

 csc /debug UncaughtException.cs<enter> 


Listing 19.1 UncaughtException.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:         Method2(); 20:         Console.WriteLine("Leaving YourClass.Method1"); 21:     } 22: 23:     public void Method2() 24:     { 25:         Console.WriteLine("Entering YourClass.Method2"); 26:         int myInt = 0; 27:         int yourInt; 28:          //Dividing by zero 29:         yourInt = 10 / myInt; 30:         Console.WriteLine("Leaving YourClass.Method2"); 31:     } 32: } Entering MyClass.Main Entering YourClass.Method1 Entering YourClass.Method2 Exception occurred: System.DivideByZeroException: Attempted to divide by zero.    at YourClass.Method2() in C:\ Temp\ TempC#Programs\ UncaughtException.cs:line 29    at YourClass.Method1() in C:\ Temp\ TempC#Programs\ UncaughtException.cs:line 19    at MyClass.Main() in C:\ Temp\ TempC#Programs\ UncaughtException.cs:line 19 

Note

graphics/common.gif

You might see a window labeled Common Language Runtime Debugging Services as you run Listing 19.1. Debugging services provide help when looking for bugs. It is beyond the scope of this book to discuss the various debuggers available in .NET.


The three methods Main, Method1, and Method2 are equipped with WriteLine statements (lines 7, 10, 18, 20, 25, and 30), so we can follow the program's execution flow in the sample output.

Execution flow initially enters the Main method as confirmed by the first line of the sample output. The Main method calls Method1 in line 9, which again calls Method2 in line 19. Under normal circumstances, Method2 would finish executing its block of code (including the call to WriteLine in line 30) and return back to Method1, which again would execute line 19 before returning to the Main method where line 10 finally would be executed before the program was terminated.

However, an abnormal condition arises in line 29 of Method2 where 10 is divided by zero. This causes the runtime to create and throw an exception object of type System.DivideByZeroException and the normal flow of execution is immediately stopped. Because line 29, which gave rise to the exception, is not placed inside a try block, Method2 cannot handle the exception object. The exception object is then sent on a journey back through the method call hierarchy, which, in this case, consists of Main calling Method1 and Method1 calling Method2. During this journey, it looks for a try block with a matching catch block, so the execution object is passed over to Method1 (without calling line 30). No try and catch blocks are found here either, so the execution object is passed on to the Main method (without executing line 20), which also lacks any exception handling code. Finally, the exception object is passed to the runtime's default exception handler, which prints the error message.

Notice how the error message of the sample output traces the journey of the exception object through the three methods by the three lines commencing with at.


   


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