Writing Custom Exceptions

   


The .NET Framework's built-in exception classes will accommodate most abnormal conditions you encounter. However, when you need additional capabilities, you can write your own custom-made exception classes by using System.ApplicationException as a base class.

Lines 3 26 of Listing 19.7 illustrate this possibility by defining a new exception class called LogarithmicException.

Listing 19.7 CustomException.cs
01: using System; 02: 03: public class LogarithmicException : System.ApplicationException 04: { 05:     private uint errorNumber; 06: 07: 08:     public LogarithmicException() : base("Logarithmic exception") 09:     { 10:         errorNumber = 1000; 11:     } 12: 13: 14:     public LogarithmicException(string message, uint initErrorNumber) : base(message) 15:     { 16:         errorNumber = initErrorNumber; 17:     } 18: 19:     public uint ErrorNumber 20:     { 21:         get 22:         { 23:             return errorNumber; 24:         } 25:     } 26: } 27: 28: class MyMath 29: { 30:     public static double CalculateLog(double num) 31:     { 32:         try 33:         { 34:             if(num < 0.0) 35:             { 36:                 throw new LogarithmicException("Logarithm of a negative  number  graphics/ccc.gifcannot be calculated", 1001); 37:             } 38:             if(num == 0.0) 39:             { 40:                 throw new LogarithmicException("Logarithm of zero is  -infinity",  graphics/ccc.gif1002); 41:             } 42:             return Math.Log(num); 43:         } 44:         catch(LogarithmicException exObj) 45:         { 46:             Console.WriteLine("Message: " + exObj.Message); 47:             Console.WriteLine("Error number: " + exObj.ErrorNumber); 48:             throw new ArithmeticException("Invalid number for Logarithm   graphics/ccc.gifcalculation"); 49:         } 50:     } 51: } 52: 53: class Tester 54: { 55:     public static void Main() 56:     { 57:         try 58:         { 59:             double number; 60:             double result; 61: 62:             Console.Write("Calculate Log for the following number: "); 63:             number = Convert.ToDouble(Console.ReadLine()); 64:             result = MyMath.CalculateLog(number); 65:             Console.WriteLine("The result is: { 0} ", result); 66:         } 67:         catch(ArithmeticException exObj) 68:         { 69:             Console.WriteLine(exObj.Message); 70:         } 71:     } 72: } 

Sample output 1:

 Calculate Log for the following number: 10<enter> The result is: 2.30258509299405 

Sample output 2:

 Calculate Log for the following number: 0<enter> Message: Logarithm of zero is -infinity Error number: 1002 Invalid number for Logarithm calculation 

Sample output 3:

 Calculate Log for the following number: -5<enter> Message: Logarithm of a negative number cannot be calculated Error number: 1001 Invalid number for Logarithm calculation 

Suppose we have developed a number system for the different exceptions in our program, so that a unique number represents each exception. For example, divide by zero has number 1004, and passing a negative value to a logarithmic calculation has number 1001. Thus, apart from storing an exception message in an exception object, we also want to store an exception number. This latter functionality is not supported by any of the intrinsic exception classes, so we decide to derive our own exception class with this ability (called LogarithmicException) from the System.ApplicationException class as specified in line 3. The errorNumber instance variable in line 5 represents the exception number. The class has two constructors:

  • A default constructor in lines 8 11 automatically assigns 1000 to the errorNumber and, by calling the base class constructor, assigns "Logarithmic exception" to the built-in string message.

  • A two-parameter constructor in lines 14 17 passes the first string parameter to the base constructor, while the second uint parameter is assigned to errorNumber in line 16.

errorNumber can be accessed through the read-only property in lines 19 25.

New objects of LogarithmicException are created and thrown in lines 36 and 40, where we not only pass the usual string argument, but also a second argument of type uint, (1001 and 1002, respectively) which represents the error number.

The errorNumber is printed to the console in line 47 of the catch block by accessing the ErrorNumber property.

Notice that instead of rethrowing exObj (as in line 48 of Listing 19.6), line 48 in Listing 19.7 demonstrates our ability to create and throw a new exception object here (of class ArithmeticException) to be handled by the outer try-catch blocks in the Main method.


   


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