Quantcast
Channel: Programming Help | Web Development and Programming Tutorials » .Net Framework
Viewing all articles
Browse latest Browse all 10

C#: Microsoft Enterprise Library: Exception Handling

$
0
0

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.

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.

1

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.

2

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.

3

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.

4

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.

5

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.

6

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.

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.

7

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.

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.

The post C#: Microsoft Enterprise Library: Exception Handling appeared first on Programming Help | Web Development and Programming Tutorials.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles



Latest Images