Displaying User-Friendly Messages

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.

You may want to replace the message in the original exception with a more appropriate, user-friendly message. To do this, you must replace the original exception with another exception that has a more appropriate message associated with it. For example, exceptions that occur in the data access layer of an application can be replaced with an exception of type System.ApplicationException. This uses the message, "The application is unable to process your request at this time." This message is then displayed to the user.

Typical Goals

You want to display a user-friendly message when an exception occurs. Your application has code in its catch blocks similar to the following.

Note

The code does not include implementations of the FormatException and Logger.Log methods. These methods represent typical ways to create and log a formatted exception message.

catch(SomeException e)
{
    string formattedInfo = FormatException(e);
    Logger.Log(formattedInfo);
    throw e;
}
'Usage
Catch e As SomeException
  Dim formattedInfo As String = FormatException(e)
  Logger.Log(formattedInfo)
  Throw e
End Try

Solution

Use either a wrap handler or replace handler to create a new exception with the appropriate message.

QuickStart

For an extended example of how to use the Exception Handling Application Block to display a user-friendly message, see the QuickStart walkthrough, Walkthrough: Notifying the User.

Displaying User-Friendly Messages

The following procedure describes how to use the Exception Handling Block to display user-friendly messages.

To display user-friendly messages

  1. Use the configuration tools to create an exception handling policy with the relevant exception types for your application. For more information, see Entering Configuration Information.
  2. Configure the exception type. Specify the PostHandlingAction as ThrowNewException. The ThrowNewException action indicates that the application block will throw the exception that is returned from the last exception handler in the chain.
  3. Add a new wrap exception handler for the specified exception types.
  4. Configure the wrap exception handler with the new exception type and friendly message.
  5. Modify your application code to execute the new policy when an exception occurs.

Usage Notes

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.