Throwing an Exception


The preceding examples have been catching exceptions generated automatically by C#. However, it is possible to manually throw an exception by using the throw statement. Its general form is shown here:

 throw exceptOb;

The exceptOb must be an object of an exception class derived from Exception.

Here is an example that illustrates the throw statement by manually throwing a DivideByZeroException:

 // Manually throw an exception. using System; class ThrowDemo {   public static void Main() {     try {       Console.WriteLine("Before throw.");       throw new DivideByZeroException();     }     catch (DivideByZeroException) {       // catch the exception       Console.WriteLine("Exception caught.");     }     Console.WriteLine("After try/catch block.");   } }

The output from the program is shown here:

 Before throw. Exception caught. After try/catch block.

Notice how the DivideByZeroException was created using new in the throw statement. Remember, throw throws an object. Thus, you must create an object for it to throw. That is, you can’t just throw a type. In this case, the default constructor is used to create a DivideByZeroException object, but other constructors are available for exceptions.

Most often, exceptions that you throw will be instances of exception classes that you created. As you will see later in this chapter, creating your own exception classes allows you to handle errors in your code as part of your program’s overall exception handling strategy.

Rethrowing an Exception

An exception caught by one catch statement can be rethrown so that it can be caught by an outer catch. The most likely reason for rethrowing an exception is to allow multiple handlers access to the exception. For example, perhaps one exception handler manages one aspect of an exception, and a second handler copes with another aspect. To rethrow an expression, you simply specify throw, without specifying an exception. That is, you use this form of throw:

 throw ;

Remember, when you rethrow an exception, it will not be recaught by the same catch statement. It will propagate to a catch statement of an outer try.

The following program illustrates rethrowing an exception. In this case, it rethrows an IndexOutOfRangeException.

 // Rethrow an exception. using System; class Rethrow {   public static void genException() {     // here, numer is longer than denom     int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 };     int[] denom = { 2, 0, 4, 4, 0, 8 };     for(int i=0; i<numer.Length; i++) {       try {         Console.WriteLine(numer[i] + " / " +                           denom[i] + " is " +                           numer[i]/denom[i]);       }       catch (DivideByZeroException) {         // catch the exception         Console.WriteLine("Can't divide by Zero!");       }       catch (IndexOutOfRangeException) {         // catch the exception         Console.WriteLine("No matching element found.");         throw; // rethrow the exception       }     }   } } class RethrowDemo {   public static void Main() {     try {       Rethrow.genException();     }     catch(IndexOutOfRangeException) {       // recatch exception      Console.WriteLine("Fatal error -- " +                        "program terminated.");     }   } }

In this program, divide-by-zero errors are handled locally, by genException( ), but an array boundary error is rethrown. In this case, the IndexOutOfRangeException is handled by Main( ).




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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