Click here to Skip to main content
6,295,667 members and growing! (12,138 online)
Email Password   helpLost your password?
General Programming » Exception Handling » General     Intermediate License: The Code Project Open License (CPOL)

Automatic Error Handling

By Andy Searls

Handle web and WinForms exceptions automatically.
C#, .NET (.NET 2.0), ASP.NET, Dev
Posted:17 Mar 2008
Views:13,411
Bookmarked:42 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 3.70 Rating: 4.38 out of 5

1

2
2 votes, 28.6%
3
1 vote, 14.3%
4
4 votes, 57.1%
5

Introduction

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.

Background

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:

  • Instead of configuring which destination(s) to log errors, I added a fallback algorithm. Logging to the database gets priority, but if that fails, the local event log is a backup. If that fails, then saving to a local file is a last resort. The error is always sent via email.
  • The separate web form and Windows form libraries are easily combined. I’ve also rolled it into a library of common utilities, providing all the extra features I often look for in a single project. Moving the common methods into a common base class helps with maintenance.
  • Some of the internal fallback exception handlers were not very helpful in documenting the secondary error, and event handling results were not always well handled. I tweaked both to provide a little more information.
  • The pretty call stack formatting was showing parameters in VB format. To be consistent, I changed it to C# syntax.
  • SMTP email handling classes were added to the framework in version 2.0. I replaced the use of the hand-crafted classes with the framework classes. I also looked up the settings to use Gmail to send the alerts. This can be very handy when initially setting up an application and your client doesn’t have their servers up and running, yet.
  • There are times when the application can handle the exception, but you want the problem logged for later analysis. I added an overload that allows error handling and logging without interrupting the user or halting the application.

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.

Using the code

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.

Footnotes

  1. MSDN: Best Practices for Handling Exceptions
  2. www.codinghorror.com
  3. The original articles can be found under Jeff Atwood's postings - User Friendly Exception Handling

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Andy Searls


Member

Occupation: Software Developer (Senior)
Company: Direct Technology
Location: United States United States

Other popular Exception Handling articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralAnonymous assemblies throw an exception PinmemberPentoflex23:11 6 Aug '08  
GeneralYou`re discarding source information PinmemberPentoflex2:35 6 Aug '08  
GeneralRe: You`re discarding source information PinmemberAndy Searls8:40 26 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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