Displaying User-Friendly Messages

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

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. The code does not include definitions of the FormatException and Logger.Log methods. These methods represent typical ways to create and log a formatted exception message.

C# Copy Code
catch(SomeException e)
  string formattedInfo = FormatException(e);
  throw e;
Visual Basic Copy Code
Catch e As SomeException
  Dim formattedInfo As String = FormatException(e)
  Throw e
End Try


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

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 post-handling action as ThrowNewException. The ThrowNewException action indicates that the 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. The code shown here assumes you have resolved an instance of the ExceptionManager class through the Enterprise Library container and saved it in a variable named exManager. For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.
    C# Copy Code
    exManager.Process(YourTargetMethod, "Notify Policy");
    Visual Basic Copy Code
    exManager.Process(AddressOf YourTargetMethod, "Notify Policy")