Trang

Thứ Năm, 30 tháng 5, 2013

Chapter 16: Custom Exceptions

The Microsoft .NET team has done a good job by providing us a rich set of Exception classes. Most of the time, these exception classes are sufficient enough to handle any common error situation. However, it may be required that our application would need some additional exception classes, that are not available in .NET Framework.

The .NET Framework exception handling mechanism allows us to create our own custom exception classes and use it in our applications.

Custom Exceptions

All exceptions in .NET are derived from the root exception class - System.Exception. There are two other
exception classes immediately derived from the System.Exception :


  • System.SystemException

  • System.ApplicationException


  • All exceptions thrown by the .NET Framework is derived from the SystemException class. If we raise any exception from our application, it should be derived from the System.ApplicationException. This will differentiate our exceptions from the system generated exceptions.

    It is easy to create a custom exception class. Just create a new class and mark it as derived from System.ApplicationException

    public class UserNotExistsException : System.ApplicationException 
     {
     }   



    Now we have derived a custom exception class and we can use it in ourr application. See the sample code, showing how we 
    can throw ourr custom exception.
     public void UpdateUser( User userObj )
     {
      if ( some error condition )
      {
       throw new UserNotExistsException();
      }
     }


    The 'throw' statement allows us to raise an exception programmatically. Here we are creating a new object of type 'UserNotExistsException' and throwing it. Another class or method which calls our method should handle this exception.
     try
     {
      UpdateUser( userObj );
     }
     catch ( UserNotExistsException ex )
     {
      MessageBox.Show ( ex.Message );
     }
       

    In this sample, we are calling the method UpdateUser(...), which might throw an exception of type UserNotExistsException. If this exception is raised, it will go to our catch block and we are showing a message to the user. 

    Enhancing our custom exception

    In the custom exception we defined above, we haven't specified any constructor. But since we derive it from the ApplicationException, it will use the public constructors available in AppicationException. It is a good idea to provide at least one constructor, which takes an error message as a parameter.
     public class UserNotExistsException : System.ApplicationException 
     {
      public UserNotExistsException ( string message ) : base ( message )
      {
      }
     }   


    Now, when we throw this exception, we have to pass an error message to the constructor. The error handlers will be able to retrieve this message from the Exception object and show a better message to the user.
     public void UpdateUser( User userObj )
     {
      if ( some error condition )
      {
       throw new UserNotExistsException( "The user you are trying to update does not exists." );
      }
     }


    See the exception handler code :
     try
     {
      UpdateUser( userObj );
     }
     catch ( UserNotExistsException ex )
     {
      MessageBox.Show ( ex.Message );
     }

    Now the user will see the more friendly error message which we passed as part of the exception, while throwing it.

    You can customize the custom exception classes such a way that it passes any information you want including the date and time the exception occurred, the method name in which the exception was first raised, user name of the current user etc. You may also provide a Save() method for your custom exception class so that when a caller catch our custom exception, he can call the Save() method, which will record the error information to the disk

    Không có nhận xét nào: