Adding Application Code
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
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 Reference. Click the Browse tab, select the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll assembly, and then click OK.
Following the same procedure, set a reference to the following assemblies,
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.dll
- Microsoft.Practices.Unity.Interception.dll
If you configure your application to use the Logging Exception Handler, set a reference to Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll and 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.
(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:
- Obtain an instance of the ExceptionManager class. For more details, see Creating Application Block Objects.
- Catch an exception.
- Process an exception policy.
- Re-throw the original exception where appropriate.
The following topics explain how to incorporate these steps into an application: