Export (0) Print
Expand All

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.

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

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.

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.

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.
    public class AppTextExceptionFormatter : TextExceptionFormatter
    {
      public AppTextExceptionFormatter(TextWriter writer, Exception exception, Guid handlingInstanceID): base (writer, exception, handlingInstanceID) 
      {
      }
    
      protected override void WriteStackTrace(string stackTrace) 
      {
      }
    
      protected override void WriteExceptionType(Type exceptionType) 
      {
        base.Indent();
        base.Writer.WriteLine("Type : {0}", exceptionType.FullName);
      }
    }
    
    
  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.
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class AppMessageExceptionHandler : IExceptionHandler
    {
      public AppMessageExceptionHandler(NameValueCollection ignore)
      {
      }
    
      public Exception HandleException(Exception exception, Guid handlingInstanceId)
      {
        DialogResult result = this.ShowThreadExceptionDialog(exception);
    
        return exception;
      }
    
      // Creates the error message and displays it.
      private DialogResult ShowThreadExceptionDialog(Exception ex) 
      {
        string errorMsg = "The following exception was caught by the QuickStart Global Exception Handler:" 
                        + Environment.NewLine + Environment.NewLine;
    
        StringBuilder sb = new StringBuilder();
        StringWriter writer = new StringWriter(sb);
    
        AppTextExceptionFormatter formatter = new AppTextExceptionFormatter(writer, ex, handlingInstanceID);
    
        // Format the exception.
        formatter.Format();
    
        errorMsg +=  sb.ToString();
    
        return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
      }
    }
    
    
  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.
    try
    {
      // Run code.
    }
    catch(Exception ex)
    {
      bool rethrow = ExceptionPolicy.HandleException(ex, "Global Policy");
      if (rethrow)
        throw;
    }
    
    

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.


Show:
© 2014 Microsoft