Scott Hanselman shares some rules of thumb to manage Exceptions gracefully in your applications. As usual, great stuff from Scott. Here are some of my own I would add:

  • Don't do: throw new Exception("Something Happened");. Please, I beg of you, don't do it. Don't abuse System.InvalidOperationException, either.
  • Chain exceptions. Seriously, chaining exceptions via the InnerException property is a great way to add new information to a problem you caught, but still allow the developer to trace all the way back to the original error to diagnose it.
  • Don't just catch exceptions and translate them to other types without adding meaningful information and  (yes, I'm repeating myself here) using InnerException. I can't remember the number of times I've seen such crap as:
  • try
    } catch(SomeException ex)
    throw new InvalidOperationException(ex.Message);

  • If you're gonna log the exception, do the right thing and log the entire exception information (just calling ToString() usually does the trick). For goodness sake don't just log Exception.Message.
  • Be sure to define and take advantage of portions of the code that are natural Exception handling boundaries, such as threaded methods. A good discussion about the topic was brought up on the comments on this entry from Jeffrey Palermo.

Tomas Restrepo

Software developer located in Colombia.