![]() |
General Programming »
Exception Handling »
General
Intermediate
License: The Code Project Open License (CPOL)
Automatic Error HandlingBy Andy SearlsHandle web and WinForms exceptions automatically. |
C#, .NET (.NET 2.0), ASP.NET, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
Microsoft’s best practices[1] regarding error handling are pretty clear. They are designed to avoid losing information that might be helpful to the developer, to provide a reasonable user experience, and to prevent a huge performance hit every time something goes wrong.
So, you’ve got your exception handlers all in a neat row, following best practices. In the inevitable event that something goes wrong, your application will properly handle the exception at one level or another.
Or will it? What about exceptions that happen outside your code? The obvious answer is an unhandled exception handler, but writing those can be tricky work, as any problems there will bring your application to a screeching halt.
And, once you have the exception in some sort of handler, then what? You clearly don’t want your carefully crafted error information to simply fall into a bit bucket. Relying on the user to tell you the exception call stack is not a good idea.
Instead of re-creating something that has been written countless times, I decided to look around to see what’s already been done. Like many enterprise, system, or application aspects, error handling and reporting does fairly common things, regardless of the purpose of the software in question. In a search for something else, I came across Jeff Atwood’s blog [2] entry for exception handling.
Atwood’s example is in VB. A majority of my work is in C#, so while I was reviewing the code, I translated it into C#. I also came up with a number of improvements to add to it:
Integration with enterprise aspects is simple enough. In fact, having a framework for configuration, logging, and database access makes logging to the database quite easy.
The solution provided includes four projects. Three of those are examples of use in console, web, and Windows Forms applications. The fourth is the error handler library itself.
The key for the console and Windows Forms applications is the registration of the unhandled exception event delegate. This is handled in a simple method call:
ErrorHandler.ExceptionHandler.AddHandler( true, true, true );
The parameters determine the default behavior for unhandled exceptions.
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Mar 2008 Editor: Smitha Vijayan |
Copyright 2008 by Andy Searls Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |