Click here to Skip to main content
Licence CPOL
First Posted 18 Dec 2007
Views 11,559
Bookmarked 13 times

Handling Specific Exceptions in the Web.config

By | 18 Dec 2007 | Article
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)

About the Author

call_vino

Web Developer

Unknown

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionHandling Specific Exceptions in the Web.config Pinmemberbhargavideepthi3:21 20 Jun '11  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 19 Dec 2007
Article Copyright 2007 by call_vino
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid