Walkthrough: Notifying the User

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 send a friendly exception message to the user and still allow support staff to access information regarding the originating exception. Figure 1 illustrates the process.

Ff649660.f8167fa4-b651-4f48-b597-79f2c4dea04d(en-us,PandP.10).png

Figure 1
Notifying the user

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

The Notify Policy specifies that exceptions of type BusinessLayerException will be replaced with exceptions of type ApplicationException. The policy is configured so that exceptions of type BusinessLayerException have a PostHandlingAction of ThrowNewException. This causes the Exception Handling Application Block to throw the new ApplicationException after the last handler in the chain executes. The policy also specifies that the Exception Handling Application block will log the original exception information before replacing the BusinessLayerException.

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 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 ProcessD and ProcessAndNotify methods to the AppService class. These methods are included in the QuickStart source code.
  6. Use the Configuration Console to create a new application configuration file.
  7. Use the Configuration Console to add the Exception Handling Application Block to the application.
  8. Add a new exception policy named Notify Policy.
  9. Add the exception type BusinessLayerException to the Notify Policy.
  10. Configure the BusinessLayerException exception type to have a PostHandlingAction of ThrowNewException.
  11. Add a Logging Handler for the BusinessLayerException exception type.
  12. Use the configuration tools to add the Logging Application Block to the application.
  13. Add a replace handler for the BusinessLayerException exception type.
  14. Enter a new System.ApplicationException exception type for the replace handler to use when it replaces the BusinessLayerException.
  15. Enter the new exception message for the new exception.