Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
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" ?> <configuration> <configSections> <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary. ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/> </configSections> <exceptionHandling> <exceptionPolicies> <add name="Global Policy"> <exceptionTypes> <add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None"> <exceptionHandlers> <add name="Application Message Handler" type="ExceptionManagementBlockExample.AppExceptionHandler, ExceptionManagementBlockExample"/> </exceptionHandlers> </add> </exceptionTypes> </add> </exceptionPolicies> </exceptionHandling> </configuration>

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 .Common.Configuration; using Microsoft.Practices.EnterpriseLibrary .Common.Configuration.ObjectBuilder; using Microsoft.Practices.EnterpriseLibrary .ExceptionHandling; using Microsoft.Practices.EnterpriseLibrary .ExceptionHandling.Configuration; namespace ExceptionManagementBlockExample { [ConfigurationElementType(typeof(CustomHandlerData))] public class AppExceptionHandler : IExceptionHandler { public AppExceptionHandler( NameValueCollection ignore) {} public Exception HandleException( Exception exception, Guid correlationID) { DialogResult result = this.ShowThreadExceptionDialog(exception); //Exits the program when the user clicks Abort if (result == DialogResult.Abort) Application.Exit(); 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) { try { throw new Exception ("This is a sample exception"); } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException( ex, "Global Policy"); if (rethrow) { throw; } } }

 
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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap