Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Thursday, September 14, 2006

Processing .NET Unhandled Exceptions

Gracefully Responding to Unhandled Exceptions - Processing Unhandled Exceptions

When an unhandled exception occurs in an ASP.NET application, the visitor is, by default, shown either a "Runtime Error" or exception details page (which of the two pages is displayed depends upon the website's configuration and whether the visitor has come through localhost). Ideally, such esoteric error pages are only shown to developers; regular users should, instead, see a custom, user-friendly error page. In last week's article, Displaying User-Friendly Error Pages, we examined the steps to configure an ASP.NET web application to display custom, user-friendly web pages in response to unhandled exceptions.

The technique discussed in last week's article, however, uses a Response.Redirect() internally to redirect the user to the custom error page in the event of an unhandled exception. By redirecting the user, the context of the request is lost (since the redirect causes the browser to send a new request to the error page). Consequently, the custom, user-friendly error page lacks the ability to garner information about the error that just occurred. Such information might be desired in order to display a more informative error message.

When an unhandled exception bubbles up to the ASP.NET runtime, the application-level Error event fires. By creating an event handler for this event, we can access the error details, log the error, notify a developer, or send the user to the custom error page using Server.Transfer() (which will maintain the context and allow the custom error page to access the details of the unhandled exception).

In this article we'll examine how to create an event handler for the Error event and examine a free, open-source component for logging error details. Read on to learn more!

Find Complete Article

Post a Comment