Click here to Skip to main content
Click here to Skip to main content

Handling Specific Exceptions in the Web.config

, 18 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

<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.

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)

Share

About the Author

call_vino
Web Developer
Unknown
No Biography provided

Comments and Discussions

 
QuestionHandling Specific Exceptions in the Web.config Pinmemberbhargavideepthi20-Jun-11 4:21 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Dec 2007
Article Copyright 2007 by call_vino
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid