C#: Microsoft Enterprise Library: Exception Handling

Category: .NET Framework

C#: Microsoft Enterprise Library: Exception Handling

By: Zack Turnbow

Introduction

This next article in the series covering Microsoft’s Enterprise Library will cover exception handling. The Exception Handling Application Block (EHAB) greatly reduces the effort when implementing exception handling. It assists in helping raise exceptions to the user and/or helps to process an exception through multiple application tiers. The greatest advantage about the EHAB is it lets the developer describe exception policies in a configuration file that can be changed on the fly without having to recompile or redeploy code.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Implementation

This article will be building upon the previous article, Microsoft Enterprise Library: Logging, by integrating exception handling into the existing code. Normal exception handling uses a try catch block. The EHAB is used in conjunction with the try block as well. Where the advantage comes into play is inside the try catch block, EHAB can handle the exceptions depending on the configuration file that defines on how to handle a particular exception. The policy can be very detailed or the policy can be broad dealing with a wide range of exceptions. So keeping this in mind, some of the common uses for the EHAB are to replace the system generated exception with a custom exception, to re-throw and exception after inspecting the original exception, to log the exception along with its details, and finally wrapping the system generated exception inside another exception. To get started, the first task is to create a reference to the EHAB in the web application. Open up the project in Visual Studio and add the reference. Then in the code behind for the Default.aspx page, add the following using statement:

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

From here, the configuration file needs to be created. The configuration can be stored in the web.config or a separate configuration file. This article will store the configuration in a separate file so that any changes to the exception policy will not force a re-compile if the configuration is stored in the web.config. Create a new configuration file in Visual Studio.


[Click to see full-size]

The new file now needs to be configured to store the exception policy. This is done by using the Enterprise Exception Configuration application. Open up the application and navigate to where the new configuration file is located. Open the file up.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.


[Click to see full-size]

Next, right click on the file in the tree view in the left pain and select New – Exception Handling Block. When this option is selected a new node will be added to the configuration tree.


[Click to see full-size]

In this article, three polices will be configured to demonstrate the EHAB capabilities. The three polices will demonstrate the concepts of wrapping an exception in a new exception, replacing the system generated exception with a new policy and log the exception, and finally rethrow an exception. To create a new policy, right click on the EHAB and select New – Exception Policy. Give it a name of Rethrow Policy. Repeat these steps to create a Replace Policy, and Wrap Policy.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


[Click to see full-size]

For each of the policies, an exception type needs to be defined. For simplicity, create a Divide By Zero Exception type for each of the policies by right clicking each policy and selecting New – Exception Type.


[Click to see full-size]

Now a handler needs to be added to each exception type except the rethrow exception type. Note that an exception type can have more than one handler. Right click on each exception type select the handlers as shown below.

 


[Click to see full-size]

Now go through each handler to set the appropriate properties. Under the re-throw policy the PostHandlingAction needs to be set to NotifyRethrow, the replace policy PostHandlingAction should be set to ThrowNewException and under its handler the ReplaceExceptionType needs to be set to System.Exception, and finally the wrap policy PostHandlingAction needs to be set to ThrowNewException and the WrapExceptionType needs to be set to System.Exception. Once the these steps are completed, save and close the Enterprise Library Configuration application

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Let’s write some code to implement the EHAB. First, drag a text box onto the Default.aspx work area and set the TextMode property to MultiLine. Copy and paste it to create one more textbox. Click over to the code behind and add the following code after the existing logging code:

try
{
int i = 1;
int j = 0;
i = i/j;
}
catch(System.DivideByZeroException ex)
{
try
{
bool rethrow = ExceptionPolicy.HandleException(ex,"ReplacePolicy");
if (rethrow)
throw;
}
catch(System.Exception exNew)
{
TextBox1.Text = exNew.ToString();
TextBox2.Text = exNew.InnerException.ToString();
}
}

Now run the application to view the results. The inner exception is populated only for the wrap policy where as the replace policy will display the system exception instead of the original exception.


[Click to see full-size]

This demonstration on exception handling using the Enterprise Library Exception Handling Application Block shows how simple it is to provide an avenue of catching exceptions and manipulating and creating new exceptions to suit a developer’s requirements.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

What have we learned?

How to use the Enterprise Library Configuration application to add and configure the EHAB.
How to implement a Replace Exception Policy.
How to implement a Wrap Exception Policy.
How to implement a Rethrow Exception Policy.

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!