Declaring Your Own Exceptions


Exceptions are classes that derive from System.Exception . However, the .NET framework distinguishes between two types of exceptions: system exceptions, exceptions Microsoft defined that have to do with the .NET Framework; and application exceptions, custom exceptions for a particular application. System exceptions derive from System.SystemException . Application exceptions derive from the class System.ApplicationException . Both System.SystemException and System.ApplicationException derive from System.Exception .

To declare an exception class:

  1. Add a new class definition. You can name the class anything you wish, but as a standard convention developers add the word Exception as the last part of the name .

  2. Inherit the class from System.ApplicationException .

  3. Add properties and methods to your class that may help the developer obtain more information about the error ( Figure 11.18 ).

    Figure 11.18 In addition to having fields that report extra error information, you could add methods that try to fix the problem, for example. Exception classes are full classes and as such you can add to them any type of member that other classes could have.
     class AccountOverdrawnException :       ApplicationException {    public int AccountNumber;    public string LastCheckNumber; } 

graphics/tick.gif Tips

  • In the section titled "Obtaining Exception Information" you learned about properties like Message , Source , TargetSite , etc. You don't have to add these properties to your class by hand; they come from the base class System.Exception . However, you may want to add custom properties to provide extra information about your class. For example, if the class is AccountException , you may want to add a property like AccountNumber that tells the developer receiving the exception the account number of the account responsible for the error ( Figure 11.19 ).

    Figure 11.19 Because we're looking for a specific exception, our exception variable is already of the same type as the exception we're catching, and so we can ask for members that are specific to our exceptions class. If we were catching general exceptions, we would have to cast to our exception type first before trying to get the account number.
     try {    MakeWithdrawal(5000); } catch(AccountOverdrawnException ex) {    string acctInfo = FetchAccountInfo(  ex.AccountNumber  );    Response.Write(acctInfo); } 
  • If you're creating a number of exception classes, it's good to divide the exceptions into categories and declare base classes for each category. For example, if you have a number of exceptions that relate to Checking accounts and other exceptions that relate to Savings accounts, it's a good idea to create a base class called AccountExceptions and perhaps create separate CheckingExceptions and SavingsExceptions classes ( Figure 11.20 ).

    Figure 11.20 This is the same approach that Microsoft takesa hierarchy of exceptions. With this structure you can decide to catch all banking exceptions, or more specific kinds of banking exceptions.
     class AccountException : ApplicationException {    public int AccountNumber; } class CheckingException:  AccountException  {    public string LastCheckNumber; } class SavingsException :  AccountException  {    public bool IsPreferredCustomer; } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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