Tuesday, May 24, 2005

The Samurai Principle for Exception Handling

When a method fulfills it's responsibility, it is very clear what to return. We return the desired result object. However sometimes it is a bit unclear what to do when a method does not totally fulfill its responsibility. Do we return a null, do we return a partially populated object, or do we throw an Exception. Most of these situations can be resolved by applying the Samurai Principle - Return victorious, or not at all. If a function fulfills its responsibilities, return the appropiate result object, and if it doesn't then throw an Exception.
Can this principle have any exceptions? I cannot think of any at the moment. Do let me know if you come accross any.

No comments: