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.
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
- Create a new blank solution in Visual Studio.
- Add a Windows Application project named ExceptionHandlingBasicQuickStart to the solution.
- Add a Class Library project named ExceptionHandlingQuickStart.BusinessLayer to the solution.
- Rename the class in ExceptionHandlingQuickStart.BusinessLayer to AppService.
- Add the ProcessD and ProcessAndNotify methods to the AppService class. These methods are included in the QuickStart source code.
- Use the Configuration Console to create a new application configuration file.
- Use the Configuration Console to add the Exception Handling Application Block to the application.
- Add a new exception policy named Notify Policy.
- Add the exception type BusinessLayerException to the Notify Policy.
- Configure the BusinessLayerException exception type to have a PostHandlingAction of ThrowNewException.
- Add a Logging Handler for the BusinessLayerException exception type.
- Use the configuration tools to add the Logging Application Block to the application.
- Add a replace handler for the BusinessLayerException exception type.
- Enter a new System.ApplicationException exception type for the replace handler to use when it replaces the BusinessLayerException.
- Enter the new exception message for the new exception.