Displaying User-Friendly Messages
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.
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.
The following procedure describes how to use the Exception Handling Block to display user-friendly messages.
To display user-friendly messages
- 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.
- 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.
- Add a new wrap exception handler for the specified exception types.
- Configure the wrap exception handler with the new exception type and friendly message.
- 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.