Share via


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.

When you use the Exception Handling Application Block, a frequently required task is to notify the user when an exception occurs. (Generally, this should be completed after the message has been changed to one that is suitable for the particular user. For more information, see Displaying User-Friendly Messages.) Depending on the application type, you can do this using a Windows Forms dialog box or a Web page. When an exception cannot be handled within the application, the user must receive a notification that an error occurred, along with some guidance of what he or she should do next.

Typical Goals

You want to notify the user that an exception has occurred.

Solution

Create a custom exception handler that constructs and displays a message for the user. Establish the handler as the global exception handler for the application. Include the handler in your exception handling chain.

QuickStart

For an extended example of how to use the Exception Handling Application Block to notify the user, see the QuickStart walkthrough, Walkthrough: Notifying the User.

Notifying the User

The following procedure describes how to use the Exception Handling Block to notify the user of an exception.

To notify the user

  1. Create a class that derives from TextExceptionFormatter that will format the message for display to the user. The following code demonstrates how to create a customer formatter that does not include the stack trace information in the message.
  2. Create a custom exception handler that displays a dialog to notify the user of an exception. The following code demonstrates how to do this.
  3. Create an exception handling policy with the relevant exception types for your application.
  4. Configure the exception type. Specify the PostHandlingAction as None. The None action indicates that the application block will not take any further action after the exception handler chain runs.
  5. Add a new custom handler for the specified exception types, setting the handler type to the new handler created in step 2.
  6. Modify your application code to execute the new policy when an exception occurs. The following code demonstrates how to do this.

Usage Notes

You should never use a handler that displays a Windows Form or raises message boxes in server applications such as ASP.NET applications and Enterprise Services applications. Doing so can cause the applications to stop responding.

If you use the Unity Integration approach to create instances of objects from the Exception Handling Application Block, you must use the non-static façade named ExceptionManager instead of the ExceptionPolicy class static façade.