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
 

Explore the Enterprise Library Exception Handling Block for .NET 2.0 : Page 3

The Enterprise Library Exception Handling Block provides all the underlying plumbing code required to handle exceptions. Stop writing repetitive exception handling code and learn how to use it in your own applications to make exception handling consistent and effective.


advertisement
Using the ExceptionPolicy Class
Whenever you work with the exception handling block, you will have to deal the ExceptionPolicy class, which exposes a static method named HandleException() that lets the client application interact with the Exception Handling Block. You supply the policy as an argument. The HandleException method uses a factory to create an object of type ExceptionPolicyImpl for the supplied policy. The ExceptionPolicyImpl object has a collection of ExceptionPolicyEntry objects—one object for each exception type specified in the configuration file for the named policy. For each exception type, the ExceptionPolicyEntry object contains a collection of objects that implement the IExceptionHandler interface. The collection is ordered and provides the sequence that the exception handling block uses when executing the policy. Each object that implements the IExceptionHandler interface has associated configuration information that is specific to each type of handler.

Exception handlers are .NET classes that encapsulate exception handling logic and implement the IExceptionHandler interface defined in the Exception Handling Block. By default, the Exception Handling Block includes three exception handlers:

Wrap handler. This exception handler wraps one exception around another.

Replace handler. This exception handler replaces one exception with another.

Logging handler. This exception handler the formatting of exception information, such as the message and stack trace. The logging handler gives this information to the logging block for publication.

You can extend the Exception Handling Block if required by implementing your own handlers using the Enterprise Library Configuration Tool. The nice thing about this approach is that you don't have to modify and rebuild the application block to extend it.

Handling Exceptions Using ExceptionPolicy
To demonstrate using the Exception Handling Block, here's a simple example. You'll create a simple Windows forms application named ExceptionMgmtBlockExample. Create the project in Visual Studio, add the references listed earlier, and then open the default form in the designer and add a Command button named btnHandleException. Modify its Click event to look as follows:

private void btnHandleException_Click (object sender, EventArgs e) { try { throw new Exception ("This is a test exception"); } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException (ex, "Global Policy"); if (rethrow) { throw; } } }

In the try block, you simply throw an exception, which the catch block will catch, invoking the HandleException() method of the ExceptionPolicy class, passing the "Global Policy" as the policy name argument. As shown in the previous section, the "Global Policy" is associated with the exception handler named AppMessageExceptionHandler, whose declaration is as follows:

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 ExceptionMgmtBlockExample { [ConfigurationElementType (typeof(CustomHandlerData))] public class AppMessageExceptionHandler : IExceptionHandler { public AppMessageExceptionHandler (NameValueCollection ignore) { } public Exception HandleException (Exception exception, Guid correlationID) { DialogResult result = this. ShowThreadExceptionDialog (exception); if (result == DialogResult.Abort) Application.Exit(); return exception; } private DialogResult ShowThreadExceptionDialog (Exception e) { string errorMsg = e.Message + Environment.NewLine + Environment.NewLine;

 
Figure 2. Sample Exception: Here's the exception you get from running the sample Windows application.
return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } }
As you can see, the custom handler derives from the IExceptionHandler interface. The HandleException method invokes another helper method named ShowThreadExceptionDialog, which formats the exception message and displays it on the screen.

If you launch the Windows application and click on the "Handle Exception" button, you will see the message box shown in Figure 2.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap