| Exception-handling in ASP.NET |
| Section: ASP.NET | Rating: 2.9 |
 | CodeGod submitted this resource The member's homepage is http://www.codegod.de Visit the profile here |
Introduction
| Like in other .NET-applications, exception-handling is also available in ASP.NET-applications. There are a few extensions and special features for exception-handling in ASP.NET that will be explained in this article: Predefined error-handlers, custom error-pages and IHttpModules. | |
Normal exception-handling
Of course your can use the known try/catch/finally-cascade of exception handling in your code:
try
{
File.OpenRead( path );
}
catch( System.IO.FileNotFoundException ex )
{
// handle error
}
finally
{
// cleanup
}
But in ASP.NET-applications you have got some extended kinds of error-handling-techniques at hand:
Error handlers
You can use predined error-handlers in your aspx-Page or in Global.asax-file. Here you can catch errors that occur in special scopes of your web-application. To get the Exception that occurred, you can use the method GetLastError() of the Server-property.
// Define it in your aspx-page
private void Page_Error(object sender, System.EventArgs e)
{
Exception ex = Server.GetLastError();
}
// Define it in Global.asax
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
}
This is very useful, cause in a lot of cases, your don't want to implement exception-handling for every block in your code where it could occur. An often used scenario is catching your exeption in Application_Error and redirect to a special page, to offer a user-friendly error-page. Therefore you can use the technique above and redirect manually via code, or you can use the ASP.NET-feature explained in the next paragraph.
Custom Error Pages
The redirection to a custom error-page can be coded but it is more handy to do it by configuration. Here is how you can configure it in your web.config:
<customErrors mode="On" defaultRedirect="Error.htm"/>
If an unhandled exception occurs, the user will be redirected to Error.htm, a page that you can define and design as you like. But you can use more detailled configuration for this. On known errors you can navigate to special error pages. An example for this is, when a page the user navigates to does not exist. Then the error 404 occurs and this is how you can tell ASP.NET to go to a special error-page for that:
<customErrors mode="On" defaultRedirect="Error.htm">
<error statusCode="404" redirect="MyErrorPage404.aspx" />
</customErrors>
Nice feature, isn't it? If you don't want to declare your error-page globally, you can also define it in the Page-directive of your aspx-file:
<%@ Page language="c#" Codebehind="Errors.aspx.cs"
AutoEventWireup="false"
Inherits="TestExceptions.Errors"
errorPage="MyErrPage.aspx" %>
Now the user is redirected to the page defined here, but only if an error in this special page occurs.
Error-handling in IHttpModule
Another possiblity to handle errors is in an own IHttpModule. To define it, you have to implement the interface IHttpModule and bind your error-handler in this class:
public class MyErrorModule : IHttpModule
{
public MyErrorModule()
{
}
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
Exception e = HttpContext.Current.Server.GetLastError();
// handle exception
// Clear the error so that it doesn't occur again in
// the Application_Error-handler
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Redirect("ErrorPage.htm");
}
#endregion
}
You also need to configure this HttpModule in your web.config-File:
<httpModules>
<add type="MyErrorModule, MyWebApp" name="MyErrorModule "/>
</httpModules>
So what is the benefit of using an own HttpModule. Well, the name already says it: You have a modularization for your error-handling. You can change your implementation at one place without touching your application-logic and you can change the errorhandling of your appliation by defining another HttpModule and configuring it in your web.app.
Okay folks, catch errors, or even better - avoid them before they appear:-)