12.8. User-Defined Exception ClassesIn many cases, you can use existing exception classes from the .NET Framework Class Library to indicate exceptions that occur in your programs. However, in some cases, you might wish to create new exception classes specific to the problems that occur in your programs. User-defined exception classes should derive directly or indirectly from class ApplicationException of namespace System. Good Programming Practice 12.1
Software Engineering Observation 12.3
Figures 12.6 and 12.7 demonstrate a user-defined exception class. Class NegativeNumberException (Fig. 12.6) is a user-defined exception class representing exceptions that occur when a program performs an illegal operation on a negative number, such as attempting to calculate its square root. Figure 12.6. ApplicationException derived class thrown when a program performs an illegal operation on a negative number.
Figure 12.7. SquareRootForm class throws an exception if an error occurs when calculating the square root.
According to Microsoft's "Best Practices for Handling Exceptions [Visual Basic]," user-defined exceptions should extend class ApplicationException, have a class name that ends with "Exception" and define three constructors: a parameterless constructor; a constructor that receives a String argument (the error message); and a constructor that receives a String argument and an Exception argument (the error message and the inner exception object). Defining these three constructors makes your exception class more flexible, allowing other programmers to easily use and extend it. NegativeNumberExceptions most frequently occur during arithmetic operations, so it seems logical to derive class NegativeNumberException from class ArithmeticException. However, class ArithmeticException derives from class SystemExceptionthe category of exceptions thrown by the CLR. Recall that user-defined exception classes should inherit from ApplicationException rather than SystemException. Class SquareRootForm (Fig. 12.7) demonstrates our user-defined exception class. The application enables the user to input a numeric value, then invokes method SquareRoot (lines 614) to calculate the square root of that value. To perform this calculation, SquareRoot invokes class Math's Sqrt method, which receives a Double value as its argument. Normally, if the argument is negative, method Sqrt returns NaN. In this program, we would like to prevent the user from calculating the square root of a negative number. If the numeric value that the user enters is negative, the SquareRoot method throws a NegativeNumberException (lines 910). Otherwise, SquareRoot invokes class Math's method Sqrt to compute the square root (line 12). When the user inputs a value and clicks the Calculate Square Root button, the program invokes event handler SquareRootButton_Click (lines 1737). The try statement (lines 2336) attempts to invoke SquareRoot using the value input by the user. If the user input is not a valid number, a FormatException occurs, and the Catch block in lines 2831 processes the exception. If the user inputs a negative number, method SquareRoot throws a NegativeNumberException (lines 910); the Catch block in lines 3235 catches and handles this type of exception. |