Adding a New Exception Handler

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

You extend the Exception Handling Application Block through designated extension points. Typically, these are custom classes written by you that implement a particular interface or derive from an abstract class. Because these custom classes exist in your application space, you do not have to modify or rebuild the Exception Handling Application Block; instead, you can designate your extensions through configuration settings.

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 class attribute ConfigurationElementType. 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.

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

  public Exception HandleException(Exception exception, Guid handlingInstanceId)
  {
     // Perform processing here. The exception returned will be passed to the next
     // exception handler in the chain. 
  }
}
'Usage
<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 returned will be 
    ' passed to the next exception handler in the chain.  
  End Function

End Class