Displaying User-Friendly Messages

07/25/2009

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.

C# Copy Code 
catch(SomeException e)
{
    string formattedInfo = FormatException(e);
    Logger.Log(formattedInfo);
    throw e;
}

Visual Basic Copy Code 
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

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 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.
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.C# Copy Code 
try
{
  // Run code.
}
catch(Exception ex)
{
  bool rethrow = ExceptionPolicy.HandleException(ex, "Wrap Policy");
  if (rethrow)
    throw;
}

Visual Basic Copy Code 
Try
  ' Run code.
Catch ex As Exception
  Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Wrap Policy")
  If (rethrow) Then
' throw original exception
    Throw
  End If
End Try

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.

Posted in: C# and .NET| Tags: Display Enterprise Library ExceptionPolicy User-Friendly Message handler Unity Integration static Facade

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions