Adding Application Code

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 Exception Handling Application Block is designed to support the most common scenarios for handling exceptions. When you add your application code, refer to the scenarios in Key Scenarios and select the ones that best suit your situation. Use the code that accompanies the scenario either as it is shown here or adapt it as required.

To prepare your application to use the Exception Handling Application Block

  1. Add a reference to the Exception Handling Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab, select the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll assembly, and then click OK.

  2. Following the same procedure, set a reference to the Enterprise Library core assemblies, Microsoft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.ObjectBuilder2.dll. If you configure your application to use the Logging Exception Handler, set a reference to the required Logging Application Block assemblies. For information about the Logging Application Block assemblies, see Adding Application Code in the documentation for The Logging Application Block.

  3. (Optional) To use elements from the Exception Handling Application Block without fully qualifying the element reference, you can add the following using statement (C#) or Imports statement (Visual Basic) to the top of your source code file.

    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    
    'Usage
    Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
    

Note

For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.

Next, add the application code. Generally, code that uses the Exception Handling Application Block must complete the following steps:

  • Catch an exception.
  • Process an exception policy.
  • Rethrow the original exception if that is what the policy recommends.

The scenarios in this section demonstrate how to incorporate these steps into an application.