Export (0) Print
Expand All
4 out of 6 rated this helpful - Rate this topic

Introduction to the Exception Handling Application Block

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.

The Enterprise Library Exception Handling Application Block lets developers and policy makers create a consistent strategy for processing exceptions that occur in all architectural layers of an enterprise application. It does this in the following ways:

  • It supports exception handling in all architectural layers of an application and is not limited to service interface boundaries.
  • It allows exception handling policies to be defined and maintained at the administrative level so that policy makers, who might be system administrators as well as developers, can define how to handle exceptions. They can maintain and modify the rules that govern exception handling without changing the application block code.
  • It provides commonly used exception handling functions, such as the ability to log exception information, the ability to hide sensitive information by replacing the original exception with another exception, and the ability to maintain contextual information for an exception by wrapping the original exception inside of another exception. These functions are encapsulated in .NET classes named exception handlers.
  • It can combine exception handlers to produce the desired response to an exception, such as logging exception information followed by replacing the original exception with another.
  • It lets developers create their own exception handlers.
  • It invokes exception handlers in a consistent manner. This means that the handlers can be used in multiple places within and across applications.

Using Exception Handlers

The Exception Handling Application Block is designed to support the typical code contained in catch statements in application components. Instead of repeating this code (such as logging exception information) throughout identical catch blocks in an application component, the application block allows developers to encapsulate this logic as reusable exception handlers. Exception handlers are .NET classes that encapsulate exception handling logic and implement the Exception Handling Application Block interface named IExceptionHandler. The Exception Handling Application Block includes four exception handlers:

  • Wrap handler. This exception handler wraps one exception around another.
  • Replace handler. This exception handler replaces one exception with another.
  • Logging handler. This exception handler formats exception information, such as the message and the stack trace. Then the logging handler gives this information to the Enterprise Library Logging Application Block so that it can be published.
  • Fault Contract Exception Handler. This exception handler is designed for use at Windows Communication Foundation (WCF) service boundaries, and generates a new Fault Contract from the exception.

Users can extend the Exception Handling Application Block by implementing their own handlers. The Configuration Console provides the ability to configure the Exception Handling Application Block to use custom handlers. Developers do not have to modify the application block source code or rebuild the application block.

Using Exception Policies

The Exception Handling Application Block lets you associate exception types with named policies. (For more information about exception types, see the .NET System.Exception class.) You do this by using the configuration console. Policies specify the exception handlers that execute when the application block processes a particular exception type. You can chain these handlers together so that a series of them execute when the associated exception type is handled. The following are some examples of named policies and descriptions of what they might provide:

  • Base policy. This policy logs the exception and rethrows the original exception.
  • Secure policy. This policy logs the exception, replaces the original exception with a custom exception, and throws the new exception.
  • Expressive policy. This policy wraps the original exception inside another exception and throws the new exception.

Example Application Code

The following code shows how to execute the policy named "Data Access Policy" when an exception occurs

try
{
  // Run code.
}
catch(Exception ex)
{
  bool rethrow = ExceptionPolicy.HandleException(ex, " Data Access Policy");
  if (rethrow)
    throw;
}

Audience Requirements

This guide is intended for software architects, software developers, and policy makers. To benefit fully from this guide, you should understand the following technologies:

  • Microsoft Visual Studio 2005 development system
  • Microsoft .NET Framework 2.0.

New Features

The May 2007 release of the Exception Handling Application Block includes the following improvements:

  • The Exception Handling Application Block can be used to implement exception shielding at the service interface of a WCF application. The ExceptionShieldingAttribute can be defined on a service implementation class to invoke an exception policy at the service boundary. The FaultContractExceptionHandler can be used to map exceptions to Fault Contracts and to prevent sensitive information from being returned to untrusted callers.
  • The ExceptionPolicy.HandleException method includes a new overload which calls the exception policy and returns the resulting exception in an out parameter, rather than throwing the resulting exception.
  • The Wrap and Replace exception handlers include additional configuration properties that allow you to specify exception messages in external resources instead of directly in the configuration file. This capability can be used to support localization of exception messages.

System Requirements

The requirements to run the Exception Handling Application Block are the following:

  • Microsoft Windows XP Professional, Windows Server 2003, or Windows Vista operating system
  • Microsoft .NET Framework 2.0 or 3.0. .NET Framework 3.0 is required for WCF Exception Shielding.
  • Microsoft Visual Studio 2005 development system (any of the following editions):

    Microsoft Visual Studio 2005 Standard Edition

    Microsoft Visual Studio 2005 Professional Edition

    Microsoft Visual Studio 2005 Team Edition for Software Developers

    Microsoft Visual Studio 2005 Team Edition for Software Testers

    Microsoft Visual Studio 2005 Team Edition for Software Architects

    Microsoft Visual Studio 2005 Team Suite

Exception Handling Application Block Dependencies

The Exception Handling Application depends on other code included in the Enterprise Library:

  • Core library functionality. The Enterprise Library Core provides services such as instrumentation and configuration and is a shared dependency of all Enterprise Library application blocks. The core library functionality is contained in the assembly Microsoft.Practices.EnterpriseLibrary.Common.dll.
  • The ObjectBuilder subsystem. The ObjectBuilder subsystem performs all the repetitive and necessary tasks for creating and disposing object instances, while still providing a high level of flexibility. Enterprise Library uses the ObjectBuilder subsystem for tasks such as injecting configuration into block classes and connecting instrumentation classes to application blocks. The ObjectBuilder subsystem is contained in the assembly Microsoft.Practices.ObjectBuilder.dll.
  • If you use the Logging Application Block to log exceptions then you need the Microsoft.Practices.EnterpriseLibrary.Logging.dll and the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll.

    In addition, the Exception Handling Application Block might have dependencies on other code, including the Logging Application Block and the Data Access Application Block. The Exception Handling Application Block requires the Logging Application Block when it uses the logging handler. If you configure the Logging Application Block to use the database trace listener, it requires the Data Access Application Block. For more information, see the documentation for the Logging Application Block and the documentation for the Data Access Application Block.

    The recommended way to modify the configuration settings for Exception Handling Application Block is to use the Enterprise Library Configuration Console.

Exception Handling Application Block Documentation

In addition to the introduction, the documentation contains the following topics:

For more information, see the following Microsoft patterns & practices guides:

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.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.