Exception Management Rules

Link. August 31, 2006. Comments [2]. Posted in: .NET | Architecture

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.


Thursday, August 31, 2006 7:33:53 AM (SA Pacific Standard Time, UTC-05:00)
The guidelines that we practice are - don't catch an exception you don't intend to handle (and handle doesn't mean rethrowing or swallowing it). This takes care of most of the above since in general we just don't catch it and let it bubble up to a generic error handler which notifies, logs, etc (and it has the full stack trace for our viewing pleasure).

Swallowing an exception in our code bases requires a really good comment explaining exactly WHY this is necessary.

We believe that having a solid well understood exception strategy across a team is a critical aspect of quality software development.
Thursday, August 31, 2006 8:09:12 AM (SA Pacific Standard Time, UTC-05:00)
Jonathan,

I hear you man and I totally agree. Sometimes, however, people need it spelled out in more detail. Besides, couldn't let such an oportunity to show some WTF's I've seen around pass :)
Comments are closed.

About

Tomas Restrepo is co-founder of devdeo. His interests include .NET, Connected Systems, PowerShell and, lately, dynamic programming languages. More...

email: tomas@winterdom.com
msn: tomasr@passport.com
twitter: tomas_restrepo

Technorati Profile

devdeo logo

View my profile on LinkedIn

MVP logo

Syndicate

Ads


Links

Categories

Statistics

Total Posts: 1006
This Year: 76
This Month: 7
This Week: 0
Comments: 771

Blogroll

Post Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 1.9.7174.0

Sign In