ASP.NET Handling Unhandled Exceptions

We’ve had an ASP.NET website thats been playing up recently, and whilst we’ve got to the bottom of the issues reported, I decided it’d be an idea to properly implement a notifier to pass details of an unhandled exceptions.

Incredibly straight-forward. Add globax.asax into the project, and then use the Application_Error() method. The exception generated can be retrieved by:

HttpContext ctx = HttpContext.Current;

Exception lastExc = ctx.Server.GetLastError();

I’ve also found it useful to check for the presence of an inner exception, and output that as well.

The only last thing to remember is to clear the server error by calling: ctx.Server.ClearError() and then redirect to a generic error page.

The code looks something like:


void Application_Error(object sender, EventArgs e)


HttpContext ctx = HttpContext.Current;
Exception exc = ctx.Server.GetLastError();

// Log details, send email, etc… here




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s