Walkthrough: Wrapping One Exception in Another

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.

This walkthrough demonstrates the steps to wrap one exception with another before propagating the exception. Figure 1 illustrates the process.

Ff648425.42504be5-d8b1-4d19-bef5-7d57c686a828(en-us,PandP.10).png

Figure 1
Wrapping one exception in another

The QuickStart executable calls a method in the business layer assembly, and an exception occurs. The Exception Handling Application Block uses the specified policy—Wrap Policy—to handle the exception.

The Wrap Policy specifies that exceptions of type DbConcurrencyException will be wrapped inside exceptions of type BusinessLayerException. The policy is configured so that exceptions of type DbConcurrencyException have a PostHandlingAction of ThrowNewException. This causes the Exception Handling Application Block to throw the new BusinessLayerException after the last handler in the chain executes.

The QuickStart executable catches this exception and calls ProcessUnhandledException. This routine calls HandleException and passes the policy named Global Policy. This policy is configured to run a custom exception handler that displays a dialog box that contains the exception message.

To reproduce this scenario

  1. Create a new blank solution in Visual Studio.
  2. Add a Windows-based application project named ExceptionHandlingBasicQuickStart to the solution.
  3. Add a class library project named ExceptionHandlingQuickStart.BusinessLayer to the solution.
  4. Rename the class in ExceptionHandlingQuickStart.BusinessLayer to AppService.
  5. Add the ProcessB and ProcessWithWrap methods to the AppService class. These methods are included in the QuickStart source code.
  6. Use the configuration tools to create a new application configuration file.
  7. Use the configuration tools to add the Exception Handling Application Block to the application.
  8. Add a new exception policy named Wrap Policy.
  9. Add the exception type DBConcurrencyException to Wrap Policy.
  10. Configure the DBConcurrencyException exception type to have a PostHandlingAction of ThrowNewException.
  11. Add a wrap handler for the DBConcurrencyException exception type.
  12. Enter a new exception type for the wrap handler to use for wrapping the DBConcurrencyException.
  13. Enter the exception message that will serve as the message of the outer exception.