Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET

Handling Specific Exceptions in the Web.config

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
18 Dec 2007CPOL 27K   7   14   1
Handling exceptions with the Enterprise Library.

Introduction

My aim is to provide the simplest way to handle exceptions in the web.config and to redirect the client when particular exceptions occur.

Background

When I was surfing the net for methods on handling exceptions in web.config... I found the Enterprise Library which make the task simple. We use the Enterprise Library Exceptions Handling Application Block to:

  • Re-throw the original exception after investigation
  • Replace the original exception with another exception
  • Log the exception details

Using the code

Step 1

I have created a class called ErrorHandling and made it as a DLL, so we can use it directly in ASP.NET applications. Just include this DLL in the bin directory of your ASP.NET application.

VB
Imports System.Web
Imports System.Collections.Specialized
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration
<ConfigurationElementType(GetType(CustomHandlerData))> _
Public Class CustomExcepetion
    Implements IExceptionHandler
    Public val As String
    Public Sub New(ByVal Url As NameValueCollection)
        val = Url.Item("url")
    End Sub

    Public Function HandleException(ByVal exception As System.Exception, _
           ByVal handlingInstanceId As System.Guid) _
           As System.Exception Implements _
           Microsoft.Practices.EnterpriseLibrary.
              ExceptionHandling.IExceptionHandler.HandleException
        HttpContext.Current.Response.Redirect(val)
    End Function
End Class

Step 2

The next step is to make changes in the web.config of the ASP.NET application:

XML
<configuration>
<configSections>
 <section name="exceptionHandling" 
    type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.
          Configuration.ExceptionHandlingSettings, 
          Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, 
          Version=3.1.0.0, Culture=neutral, 
          PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
 <exceptionHandling>
  <exceptionPolicies>
  <add name="General Policy">
  <exceptionTypes>
  <add type="System.Data.SqlClient.SqlException, System.Data, 
                Version=2.0.0.0, Culture=neutral, 
                PublicKeyToken=b77a5c561934e089"
      postHandlingAction="None" name="SqlException">
  <exceptionHandlers>
   <add url="~/ErrorPage/DatabaseFailed.aspx" 
       type="ErrorHandling.CustomExcepetion, ErrorHandling, 
             Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
       name="Custom Handler" />
    </exceptionHandlers>
     </add>
    <add type="System.Net.WebException, System, Version=2.0.0.0, 
                  Culture=neutral, PublicKeyToken=b77a5c561934e089"
     postHandlingAction="None" name="WebException">
     <exceptionHandlers>
     <add url="~/ErrorPage/RemoteServerFailure.aspx" 
          type="ErrorHandling.CustomExcepetion, ErrorHandling, 
                Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
          name="Custom Handler" />
      </exceptionHandlers>
      </add>
      <add type="System.TimeoutException, mscorlib, Version=2.0.0.0, 
                    Culture=neutral, PublicKeyToken=b77a5c561934e089"
           postHandlingAction="None" 
           name="TimeoutException">
       <exceptionHandlers>
       <add url="~/ErrorPage/DatabaseFailed.aspx" 
               type="ErrorHandling.CustomExcepetion, ErrorHandling, 
                     Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
               name="Custom Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Step 3

The Application_Error event handler is specified in the Global.asax file of your application.

C#
protected void Application_Error(object sender, EventArgs e)
{
    Exception objErr = Server.GetLastError().GetBaseException();
    ExceptionPolicy.HandleException(ex, "General Policy")
    //additional actions...
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHandling Specific Exceptions in the Web.config Pin
bhargavideepthi20-Jun-11 3:21
bhargavideepthi20-Jun-11 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.