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")
}