Click here to Skip to main content
13,353,124 members (60,471 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 18 Dec 2007

Handling Specific Exceptions in the Web.config

, 18 Dec 2007
Rate this:
Please Sign up or sign in to vote.
Handling exceptions with the Enterprise Library.


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


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 _
    End Function
End Class

Step 2

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

 <section name="exceptionHandling" 

          Version=, Culture=neutral, 
  <add name="General Policy">
  <add type="System.Data.SqlClient.SqlException, System.Data, 
                Version=, Culture=neutral, 

      postHandlingAction="None" name="SqlException">
   <add url="~/ErrorPage/DatabaseFailed.aspx" 

       type="ErrorHandling.CustomExcepetion, ErrorHandling, 
             Version=, Culture=neutral, PublicKeyToken=null"

       name="Custom Handler" />
    <add type="System.Net.WebException, System, Version=, 
                  Culture=neutral, PublicKeyToken=b77a5c561934e089"

     postHandlingAction="None" name="WebException">
     <add url="~/ErrorPage/RemoteServerFailure.aspx" 

          type="ErrorHandling.CustomExcepetion, ErrorHandling, 
                Version=, Culture=neutral, PublicKeyToken=null"

          name="Custom Handler" />
      <add type="System.TimeoutException, mscorlib, Version=, 
                    Culture=neutral, PublicKeyToken=b77a5c561934e089"


       <add url="~/ErrorPage/DatabaseFailed.aspx" 

               type="ErrorHandling.CustomExcepetion, ErrorHandling, 
                     Version=, Culture=neutral, PublicKeyToken=null"

               name="Custom Handler" />

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


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


About the Author

Web Developer
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHandling Specific Exceptions in the Web.config Pin
bhargavideepthi20-Jun-11 4:21
memberbhargavideepthi20-Jun-11 4: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.

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