The throw Statement

Most of the time, the system generates exceptions for you. However, you can also generate your own exceptions, and that’s useful for the purposes of demonstration in this chapter, so we’ll show you how to do that first. To signal an abnormal condition in a C# program, you throw an exception by using the throw keyword. The following line of code creates a new instance of System.Exception and then throws it:

throw new System.Exception();

Remember that exceptions are objects in C#, not simply messages, so the throw statement here actually creates a new Exception object, which is why it looks like it’s calling a constructor. Example 16-1 illustrates what happens if you throw an exception and there is no exception handler to catch and handle the exception. In this example, you’ll throw an exception even though nothing has actually gone wrong, just to illustrate how an exception can bring your program to a halt.

Example 16-1. The unhandled exception in this example will crash your program, which is what you’re trying to avoid

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example_16_1_ _ _ _Unhandled_Exception
{
    class Tester
    {
        public void Run()
        {
            Console.WriteLine("Entering Run...");
            Method1();
            Console.WriteLine("Exiting Run...");
        }

        public void Method1()
        {
            Console.WriteLine("Entering Method1...");
            Method2();
            Console.WriteLine("Exiting Method1...");
        }

        public void Method2()
        {
            Console.WriteLine("Entering Method2...");
            throw new System.Exception();
            // this next line can never execute
            Console.WriteLine("Exiting Method2...");
        }
        static void Main()
        {
            Console.WriteLine("Entering Main...");
            Tester t = new Tester();
            t.Run();
            Console.WriteLine("Exiting Main...");
        }
    }
}

The output looks like this:

Entering Main...
Entering Run...
Entering Method1...
Entering Method2...

Unhandled Exception: System.Exception: Exception of type
'System.Exception' was thrown.
   at Example_16_1_ _ _ _Unhandled_Exception.Tester.Method2()
   in C:DocumentsVisual Studio 2008ProjectsChapter 16
   Example 16-1 -- Unhandled ExceptionExample 16-1 -- Unhandled
   ExceptionProgram.cs:line 27
   at Example_16_1_ _ _ _Unhandled_Exception.Tester.Main()
   in C:DocumentsVisual Studio 2008ProjectsChapter 16Example 16-1
   -- Unhandled ExceptionExample 16-1 -- Unhandled Exception
   Program.cs:line 35

If you’re trying this example in Windows Vista, you won’t see this output immediately. Instead, you’ll see a dialog box telling you that the program has stopped working and that Windows is searching for a solution to the problem. It won’t find one, so go ahead and click Cancel, and you’ll see the error message.

Tip

When you run this code, you’ll also receive a warning that the following line is unreachable:

Console.WriteLine( "Exiting Method2..." );

That’s because the compiler can tell that there’s no way this line will ever be reached. In this example, you can ignore the warning, but as noted earlier, you should usually try to write warning-free code.

This simple example writes to the console as it enters and exits each method. Main() calls Run(), which in turn calls Method1(). After printing out the “Entering Method1” message, Method1() immediately calls Method2(). Method2() prints out the first message and throws an object of type System.Exception.

Execution immediately stops, and the CLR looks to see whether there is a handler in Method2(). There is not, and so the runtime unwinds the stack (never printing the “exiting” statement) to Method1(). Again, there is no handler, and the runtime unwinds the stack back to Main(). With no exception handler there, the default handler is called, which prints the error message and terminates the program. Obviously, this isn’t what you want your users to see.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset