.NET - Written by typemismatch on Friday, November 23, 2007 22:16 - 0 Comments

Don’t do this: Exception Handling

Tags: ,

I work with a lot of .NET developers and hey even some Java ones from time to time ;) We’ve all done some dumb things and we all write bugs but the following is a recurring issue I find and I wish I knew where people were learning this.

try

{

    // do something bad

}

catch (Exception err)

{

    throw(err);

}

If you aren’t going to do anything with an exception then why are you catching it? Make your code user friendly and actually try recover from whatever went wrong, if it’s completely unknown then log it and let the user know (if required) that something bad happened.

As for logging techniques when things do go wrong please don’t just dump it out to a log file. I’m lazy and I don’t need to go trawling around the drive looking and then searching through log files. Here is my recommended sequence to follow when trying to log application errors:

  • For critical failures send an email but as with all logging, make sure you don’t end up in loops!
  • Log next into either the server event logs (application log) or an internal log that the site/application admin can use. Of course this won’t work if you’re experiencing a database problem in which case go back to the event log.
  • As the last resort log to the disk.

Since I write a ton of ecommerce style sites I have one fail-safe for all financial related errors - all the available data gets emailed to me so I can rebuild the required data for the user.

-c

  • Digg
  • del.icio.us
  • Facebook
  • Technorati
  • StumbleUpon


Leave a Reply

Comment

Most Popular Content

Electronics, iPhone - Aug 3, 2008 12:14 - 0 Comments

iPhone Battery Tip

More In Electronics


MultiCore - Jul 30, 2008 22:38 - 0 Comments

Here comes MultiCore

More In MultiCore


Software - Oct 20, 2008 21:04 - 0 Comments

Silverlight Tip - Initialize!

More In Software