.NET Exception Handling Guidelines: Throwing Exceptions (part 2)

Exceptions are aptly named! Throw them in exceptional circumstances. A public method represents a contract with consumers. A method should raise an exception whenever it cannot successfully complete the task that its name implies it was designed to accomplish.

  • Report execution failures by throwing exceptions; do NOT return error codes.
  • Do NOT use exceptions for the normal flow of execution, if at all possible.

Don’t do this!:

foreach (Item i in someList)

{

try

{

someOtherList.Add(i);

}

catch

{

// swallow failures, where i already exists in someOtherList…

}

}

  • Create exceptions sparingly. Wherever possible, throw existing exceptions (from the System namespaces), rather than creating new ones.
  • Create and throw custom exceptions in situations where an error condition can be handled in a more specific way than existing exceptions.
  • Always throw the most specific exception that is applicable.
  • When throwing an exception, provide a descriptive and useful message, explaining the cause of the exception. Since exception messages are only relevant when an exception is unhandled, the message text should be aimed at helping developers fix the bug.
  • Check for bad arguments supplied to methods and throw ArgumentException, ArgumentNullException and ArgumentOutOfRangeException exceptions where appropriate. Set the ParamName property when throwing one of these. Code should almost never need to catch any of the Argument exceptions.
  • Do NOT throw System.Exception or System.SystemException
  • Do NOT throw OR derive from System.ApplicationException
  • Do NOT throw CLR exceptions.

part 1, part 3 and part 4 are here

.NET Exception Handling Guidelines

As a developer, some of the books I have on hand get read and re-read (partly due to low retention!). One such book is the Framework Design Guidelines by Brad Abrams and Krzysztof Cwalina; every .NET developer should have a copy of this within arms reach! After a discussion about exception handling best practices with another developer, I went back to this book for a re-read…and coincidently the same day, Scott Hanselman had made a post about the same topic. (I’m going to post this in 4 parts rather than one large post…)

The benefits of using exception handling over error return value based reporting are:

  • Exceptions promote API consistency, because they are designed for the sole purpose of error reporting. In contrast, error return codes can be easily ignored or overlooked.
  • Exceptions integrate well with object-oriented languages. When you have no control over error return values (in operator overloads, for example), an out of band mechanism is required.
  • With exception based error reporting, error handling code does not have to be close to the point of failure. In fact, developers have a choice of where to site the error handling code.
  • Exceptions can make your production code more robust than equivalent code that ignores error return values, because more problems will be caught during testing.
  • Exceptions can have rich textual error information describing the cause of the failure rather than some obscure error number.

Although you should not use return codes to indicate failures, you can still return status information for successful operations (such as returning the number of records inserted).

part 2, part 3 and part 4 are here.

IronPython v1.0 Released

As this happened 2 days ago now, it’s probably old news! IronPython v1.0 for .NET has been released. You can download the binaries, source code, and tutorials from CodePlex here.

This latest release utilises some of the new dynamic language features added to the CLR in version 2.0 of the .NET framework.

Reading List for Debugging

I discovered Tess’s blog fairly recently. She has some great material on low-level debugging and internals. Her blog led me to this blog post with an excellent reading list of books and articles on the topic of .NET debugging.

A Daily Developer Dose of Goodness

David Lemphers has had a great idea that I think we should all try to support and contribute to. It’s a Wiki for developers, but not for the technical nuts-and-bolts stuff. It’s for all the other stuff that developers often give little priority to (and I don’t mean washing!): things like career development and life skills, people skills and presumably chatting up the opposite sex for a spot of pair programming! But seriously, I think its a great idea.

It’s almost live: Daily Developers…coming soon Drop David an email and let him know you’re interested.

The only thing I don’t like (and I realise I run the risk of being excommunicated) is that it’s hosted on Sharepoint and it just doesn’t look as calm and soothing as the standard Wiki look and feel. Does it have a standard Wiki skin?

Programming is knowledge work

The whole reason that the term ‘master craftsman’ exists, is to distinguish those people who take their craft beyond that of others. If everyone could build software to the same standards at the same speed, then we would all be machines!

Large projects are (and always have been) about managing large numbers of people with greatly differing skill levels, personalities and idiosyncracies.

It’s unreasonable to assume that what you can achieve with a small team of highly skilled, highly self-motivated individuals can necessarily be scaled up to larger teams, even with a in-house framework of standards, guidelines, tools and best practices.

Programming is knowledge work, it just doesn’t scale like manufacturing.

ASP.NET 2.0 MSDN Tutorial Series

Over at the Microsoft MSDN site there is an excellent set of ASP.NET 2.0 tutorials written by Scott Mitchell (of 4GuysFromRolla fame). These very useful and up-to-date (June 2006) tutorials walk you through creating a Data Access Layer (DAL), a Business Logic Layer (BLL), using Master pages (one of the great new features in ASP.NET 2.0) and wiring up UI elements to business objects. Each includes downloadable source code.

The series consists of:
Tutorial 1: Creating a Data Access Layer
Tutorial 2: Creating a Business Logic Layer
Tutorial 3: Master Pages and Site Navigation

Tutorial 4: Displaying Data with the ObjectDataSource
Tutorial 5: Declarative Parameters
Tutorial 6: Programmatically Setting the ObjectDataSource’s Parameter Values

Tutorial 7: Master/Detail Filtering With a DropDownList
Tutorial 8: Master/Detail Filtering With Two DropDownLists
Tutorial 9: Master/Detail Filtering Across Two Pages.
Tutorial 10: Master/Detail Using a Selectable Master GridView with a Details DetailView