Exception Handling

Andy Wardley abw at wardley.org
Fri Jul 13 08:33:54 BST 2007


Matt Sergeant wrote:
> That's not error handling, that's a booleanish function. Returning "no" 
> isn't an error condition.

Great "talk" Matt, thanks for posting it.

On a similar vein to the above, returning undef to mean "not found" or 
"declined" is also entirely acceptable.  There's a difference between being 
asked to fetch something and not finding it, and being asked to fetch 
something and failing to look for it.

    if ($product = $products->fetch(id => $id)) {
    	print $product->name(), ' costs ', $product->price(), "\n";
    }
    else {
	print "You silly arse!  There is no '$id' product\n";
    }

This code handles the found/not found case, leaving *real* errors to be thrown 
as exceptions which can be handled elsewhere.

I hadn't thought of exception handling as being a Separation-Of-Concerns type 
of thing, but it's absolutely spot on the mark.  Don't mix your in-band data 
with your out-of-band errors.

A




More information about the london.pm mailing list