Export (0) Print
Expand All

Exception Handling Application Block

 

Exception Handling Application Block

patterns & practices Developer Center

Enterprise Library

patterns & practices Developer Center

Microsoft Corporation

May 2007

Summary

This page provides an overview of the Enterprise Library Exception Handling Application Block. An application block is reusable and extensible source code-based guidance that simplifies development of common exception handling functionality in .NET Framework applications.

Contents

Introduction to the Exception Handling Application Block
Design of the Exception Handling Application Block
Getting Started
Community
Feedback and Support
Related Titles

Introduction to the Exception Handling Application Block

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.

Common Scenarios

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 Framework 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.

Example Application Code

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

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

Try 
    ' Run code. 
Catch ex As Exception 
    Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, " Data Access Policy") 
    If (rethrow) Then 
        Throw 
    End If 
End Try 
  

What's New

This 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 that calls the exception policy and returns the resulting exception in an out parameter instead of 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.

Design of the Exception Handling Application Block

The Exception Handling Application Block is designed to achieve the following goals:

  • Encapsulate the logic used to perform the most common exception handling tasks into minimal application code.
  • Relieve developers of the requirement to write duplicate code and custom code for common exception handling tasks.
  • Allow exception handling policies to be changed after they have been deployed and to ensure that changes happen simultaneously and consistently.
  • Incorporate best practices for exception handling, as described in the Exception Management Architecture Guide.

Design Highlights

Figure 1 shows the interrelationships between the key classes in the Exception Handling Application Block.

Ff648739.entlib_excephandlingappblock(en-us,PandP.10).gif

Figure 1. Design of the Exception Handling Application Block

The client code interacts with the Exception Handling Application Block through the static method HandleException. This method is on the ExceptionPolicy class. The HandleException method uses a factory to create an object of type ExceptionPolicyImpl for the named policy. The ExceptionPolicyImpl object has a collection of ExceptionPolicyEntry objects. There is one object for each exception type that is specified in the configuration file for the named policy. For each exception type, the ExceptionPolicyEntry object contains a collection of objects that implements the IExceptionHandler interface. The collection is ordered and provides the sequence that the Exception Handling Application Block uses when executing the policy. Each object that implements the IExceptionHandler interface has associated configuration information that is specific to each type of handler.

Getting Started

The Exception Handling Application Block has been developed as a result of analyzing common enterprise development challenges and successful solutions to these challenges. However, because each application is unique, you will not find this application block suitable for every application. To evaluate this application block and determine its applicability to your projects, Microsoft suggests you dedicate at least half of a day to explore the application block. The following is a suggested evaluation approach:

  1. Download Enterprise Library.
  2. Install Enterprise Library and compile all application blocks and tools.
  3. Read the "Introduction" and "Scenarios and Goals" sections of the documentation.
  4. Compile and run the QuickStart samples, and read through the related "QuickStart Walkthroughs" and "Key Scenarios" sections of the documentation.
  5. If the application block looks like a good fit for your application, try implementing a simple use case in your application or in a throw-away prototype application using the application block.

Community

Enterprise Library, like many patterns & practices deliverables, is associated with a community site. On this community site, you can post questions, provide feedback, or connect with other users for sharing ideas. Community members can also help Microsoft plan and test future deliverables, and download additional content such as extensions and training material.

Feedback and Support

Questions? Comments? Suggestions? To provide feedback about this application block, or to get help with any problems, please visit the Enterprise Library Community site. The community site is the preferred feedback and support channel because it allows you to share your ideas, questions, and solutions with the entire community.

Enterprise Library is a guidance offering, designed to be reused, customized, and extended. It is not a Microsoft product. Code-based guidance is shipped "as is" and without warranties. Customers can obtain support through Microsoft Support Services for a fee, but the code is considered user-written by Microsoft support staff. For more information about our support policy, see the Enterprise Library home page.

Related Titles

Start | Previous | Next

patterns & practices Developer Center

Show:
© 2014 Microsoft