RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using the Exception Handling Block in Enterprise Library 3.0 : Page 2

Give your applications seamless and reusable error handling and logging services with the powerful toolset from Enterprise Library 3.0.

A Basic Logging Example
Here's a basic example that shows the steps involved in configuring the Exception Handling block and using it to handle exceptions. To begin, create a new Visual C# Windows Application named ExceptionManagementBlockExample. After creating the project, navigate to the <DriveName>:\Program Files\Microsoft Enterprise Library 3.1—May 2007\Bin folder and add these two required assembly references to the project:

  • Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
  • Microsoft.Practices.EnterpriseLibrary.Common.dll
Open the default form's code file, and import the following core exception handling namespace:

   using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;  
Add an application configuration file (app.config) to the project, and modify it to look as follows:

   <?xml version="1.0" encoding="utf-8" ?>
     <section name="exceptionHandling" 
      <add name="Global Policy">
        <add name="Exception" type="System.Exception, mscorlib, 
         Version=, Culture=neutral, 
         PublicKeyToken=b77a5c561934e089" postHandlingAction="None">
         <add name="Application Message Handler" 
The preceding configuration file declares an exception policy named "Global Policy" that is implemented by a custom exception handler class named AppExceptionHandler. The custom exception handler implements the HandleException() method of the IExceptionHandler interface. Whenever a client application invokes the Exception Handling block with the global policy, the block invokes the AppExceptionHandler.HandleException() method. Here's the custom handler code:

   using System;
   using System.Collections.Specialized;
   using System.Windows.Forms;
   using Microsoft.Practices.EnterpriseLibrary
   using Microsoft.Practices.EnterpriseLibrary
   using Microsoft.Practices.EnterpriseLibrary
   using Microsoft.Practices.EnterpriseLibrary
   namespace ExceptionManagementBlockExample
     public class AppExceptionHandler : IExceptionHandler
         public AppExceptionHandler(
            NameValueCollection ignore) {}
         public Exception HandleException(
            Exception exception, Guid correlationID)
            DialogResult result = 
            //Exits the program when the user clicks Abort
            if (result == DialogResult.Abort)
            return exception;
         // Creates the error message and displays it
         private DialogResult ShowThreadExceptionDialog(Exception e)
            string errorMsg = e.Message;
            return MessageBox.Show(errorMsg, "Application Error", 
               MessageBoxButtons.OK, MessageBoxIcon.Warning);
The HandleException() method invokes a private helper method named ShowThreadExceptionDialog that displays the error information in a message box.

Now you can add some code to invoke the Exception Handling block. Add a button named btnProcessException to the default form, and modify its Click event to look as follows:

   private void btnProcessException_Click(
      object sender, EventArgs e)
         throw new Exception ("This is a sample exception");
      catch (Exception ex)
         bool rethrow = ExceptionPolicy.HandleException(
            ex, "Global Policy");
         if (rethrow)
Figure 2. Output Produced by the Custom Exception Handler Class: The ExceptionManagementBlockExample.AppExceptionHandler class inherits from the IExceptionHandler interface and implements this simple MessageBox as the custom exception behavior.

The ExceptionPolicy.HandleException() method is the core of the Exception Handling block; you invoke it to process exceptions, supplying as arguments the generated exception as well as the name of the policy you want to apply. The Exception Handling block associates the supplied policy name ("Global Policy" in this case) to the appropriate exception handler—defined by configuration file entries.

Figure 2 shows the output produced when you click on the "Process Exception" button.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date