Every now and then we need to catch exceptions and recover from those exceptions. If you have noticed there is always a common pattern in handling exceptions and than recovering from the exceptions.In this article we will see how you can catch the exception using "Enterprise Library Exception Handling Block".

 

Introduction:

Every now and then we need to catch exceptions and recover from those exceptions. If you have noticed there is always a common pattern in handling exceptions and than recovering from the exceptions. Let's see a common pattern of handling exceptions.

decimal num = 9;

decimal den = 0;

try
{

decimal result = num/den;

}

catch(Exception ex)
{

// logg the exception

}

finally
{

// close any connections and resources that are openened.

}

The above code generates an exception since the numerator is divided by denominator which is "0". You will see these patterns everywhere there is nothing wrong using using the above approach. But as you might have noticed that we need to write some what lot of lines to catch the exceptions and this is only catching the exception and not even logging them. In this article we will see how you can catch the exception using "Enterprise Library Exception Handling Block".

Using Enterprise Library Exception Handling Block:

The first thing we need to do is to add reference to the dynamic link library. You can find all the libraries associated with the Enterprise Library in the Enterprise Library Folder which is installed in the Program Files by default. Just add the reference to

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

After making the reference you can include this library in your page using the following line:

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

Once you got all the references you will need to use the "Enterprise Library Configuration Application" to add the Exception Handling Block. After you can see the Configuration Application you can select "Open" from the file menu and open the "web.config" file. Once you will open the web.config file you can see the "Application" node. Just right click on the Application Node and add a "Exception Handling Block". Once the "Exception Handling Block" been added to your application you will see something like this:

In the end of the nodes you can see the "Exception Handling Application Block" with the red "cross" sign. Now you need to handling the exceptions. This is again provided by the Enterprise Library Configuration Application. Just right click on the Exception Handling block and add "New Exception Policy".

After added the new Exception policy you can add the exception type by just right clicking on the "Exception Policy" and adding the type.

In this case we will add the "DivideByZeroException" since that is the exception type we need.

Change the name of the Exception Policy to "Divide By Zero". You can find all the possible exceptions from which you can select and the best thing is all this is done by the tool provided by the "Enterprise Library".

After saving this information using the "Save" button you will notice the web.config file changed. Let's see the contents of the web.config file now. Here is the web.config file after the entries for the "Exception Handling Block" has been added.

Now let's see the real part where we can use the Exception in our application.

private void Button1_Click(object sender, System.EventArgs e)
{

decimal num = 9;

decimal den = 0;

try
{

decimal result = num/den;

}

catch(DivideByZeroException exception)
{

bool rethrow = ExceptionPolicy.HandleException(exception,"Divide By Zero");

if(rethrow)
{

throw;

}

}
catch(Exception ex)
{

bool rethrow = ExceptionPolicy.HandleException(ex,"General Exception");

if(rethrow)
{

throw;

 }

}

}

Now we are catching the exception which will be "Divide By Zero" exception. and you can log the exception or take any other action.

Sometimes we need to replace one exception with the other. The reason can be sensitive information being sent to the users that are not suppose to see it. Here is a simple way of replacing one exception with another.

catch(SomeException e)
{
CustomException myException = new CustomException(?Unable to access the configured data source?);

throw myException;

}

How about sending one exception wrap into another exception. You can easily do this using this code pattern.

catch(SomeException e)
{
CustomException myException = new CustomException(e);

throw myException;
}

   You want to let an exception propagate up the call stack unchanged after the exception handler chain completes. Your application has code in its catch blocks similar to the following.

catch(SomeException e)
{
// Exception handling code to be done before propagating
string formattedInfo = FormatException(e);
Logger.Log(e);

throw e;
}


Sometimes we need to display user friendly messages to the users.

catch(SomeException e)
{
string formattedInfo = FormatException(e);
Logger.Log(formattedInfo);

throw e;
}

As you see Enterprise Library Exception Application Block is very useful in handling exceptions. You can not only catch the exceptions which are part of the Microsoft.net library but you can also catch the Generic type exceptions or the exceptions that are build by the user. All you need to do is to add a new Exception Policy and select the Exception type and that's it.

I hope you liked the article, happy coding!