Adding a New Exception Handler

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

To add a new exception handler, you must first create a new class that implements the IExceptionHandler interface. After you compile the new class into an assembly, you can use the Enterprise Library Configuration Console to add it to the Exception Handling Application Block configuration for your application.

Creating a New Exception Handler Class

This procedure describes how to create a custom exception handler class. The code example shows a framework you can use as a basis for the class.

To create a new exception handler class

  1. Create a new class that implements the IExceptionHandler interface.
  2. Add the attribute ConfigurationElementType to the class. Specify the type CustomHandlerData as the attribute parameter.
  3. Implement the HandleException method. The HandleException method contains the exception handling logic for the custom handler. When the method completes, it returns an exception. This can be the same exception passed to the HandleException method or it can be a new exception created by your handler. The exception returned by the HandleException method passes to the next exception handler’s HandleException method.

The following is a skeletal custom handler class.

C# Copy Code
[ConfigurationElementType(typeof(CustomHandlerData))]
public class AppMessageExceptionHandler : IExceptionHandler
{
  public AppMessageExceptionHandler(NameValueCollection ignore)
  {
  }

  public Exception HandleException(Exception exception, Guid handlingInstanceId)
  {
     // Perform processing here. The exception you return will be 
     // passed to the next exception handler in the chain.
  }
}
Visual Basic Copy Code
<ConfigurationElementType(GetType(CustomHandlerData))> _
Public Class AppMessageExceptionHandler
  Implements IExceptionHandler

  Public Sub New(ByVal ignore As NameValueCollection)

  End Sub


  Public Function HandleException(ByVal e As Exception, ByVal handlingInstanceID As Guid) As Exception _
         Implements IExceptionHandler.HandleException
    ' Perform processing here. The exception you return will be 
    ' passed to the next exception handler in the chain.  
  End Function

End Class

For detailed information about how to integrate custom providers with the Enterprise Library configuration system and configuration tools see Creating Custom Providers for Enterprise Library.