Enterprise Library: Exception Handling Block 5.0 in ASP.NET

In this article, you will learn how to use an exception Microsoft Enterprise Library 5.0 Exception Handling Application Block with real-world example in ASP.Net.

  • What is an Exception Handling Block? The Enterprise Library Exception Handling Application Block helps developers and policy makers to create a consistent strategy for processing exceptions that occur in all architectural layers of an enterprise application. It provides commonly used exception handling functions, such as the ability to log exception information, the ability to hide sensitive information by replacing the original exception with another exception, and the ability to maintain contextual information for an exception by wrapping the original exception inside another exception. These functions are encapsulated in .NET classes named exception handlers.

Read more: http://msdn.microsoft.com/en-us/library/ff649140.aspx

Install Enterprise Library

Please follow this link to download the Enterprise Library 5.0:

http://www.microsoft.com/en-in/download/details.aspx?id=15104 

To install an Enterprise Library Exception Handling Application Block, run the following command in the Package Manager Console:

PM> Install-Package EnterpriseLibrary.ExceptionHandling

Getting Started

Begin using the following procedure:

  • Start Visual Studio
  • Create a new website
  • Provide the name and location of website
  • Click "Next"

Now add a reference for the following two assemblies in the bin folder: "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll.

Now open the web.config file in edit mode and edit the exception handling settings.

Enterprise-Library1.jpg 
Image 1.

Now add Exception Handling Settings from blocks tab.

Enterprise-Library2.jpg
Image 2.

Now you need to provide policy name and exception type.

Enterprise-Library3.jpg
Image 3.

Now check that your web.config has been updated. It adds an exceptionhadling section in the section tag and exception handling tag.

  1. <configSections>  
  2.     <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />  
  3.     <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />  
  4. </configSections>  
  5. <exceptionHandling>  
  6.     <exceptionPolicies>  
  7.       <add name="UI Policy">  
  8.         <exceptionTypes>  
  9.           <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  
  10.             postHandlingAction="ThrowNewException" />  
  11.         </exceptionTypes>  
  12.       </add>  
  13.     </exceptionPolicies>  
  14. </exceptionHandling> 

Example

First, on the page you need to add this namespace:

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; 

And now you need to write a try, catch block like this:

  1. try  
  2. {  
  3.     throw new Exception("This exception created by raj");  
  4. }  
  5. catch (Exception ex)  
  6. {  
  7.     bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");  
  8.     if (rethrow)  
  9.     {  
  10.         throw;  
  11.     }  
  12. } 

"UI Policy" is your policy name which was created earlier in Figure 3.


Similar Articles