Thursday, August 21, 2003

Global Error Handling in ASP.NET

Brad Wilson has a response to my recent post. Here’s an excerpt:

I read Craig Andera's post about global error handling in .NET, and I thought I'd give an alternative implementation for global error handling.

We use a system like this to catch all unhandled exceptions, e-mail them to the admin, log them to the Event Log, and then give the user our own version of an "Internal System Error" page.

The HttpApplication object class contains an event called Error. This event is issued whenever an unhandled exception occurs. The exception that has been thrown is available via Server.GetLastError(). The easiest way to do this is by making a new class that overrides IHttpModule and registering it in Web.config, like so:

[The .NET Guy]

I like this a lot, and I think both techniques are valuable tools to have in your error handling arsenal – the way Fritz showed me gives you the ability to do per-page error handling, and Brad’s approach gives you application-level error handling. I plan to use both, especially since Brad’s way lets me catch more types of errors. On top of that, clever use of EIF can allow me to switch log messages for each of them on and off independently.




  1. I felt that WebServices were being left out of the party, so I wrote a little blurb on how you can use SOAP extensions to create a global exception-handling/logging framework. It's a bit too long for me to paster the entire thing into this comment, but you can see it at the URL below:

  2. One thing I haven't seen mentioned is the use of Microsoft's Exception Application Block to handle ASP.NET errors. We are using this in our Global.asax.cs' Application_Error function to catch unhandled exceptions.