For the past many years I've been using Exception::Class to create rich, object-oriented exceptions. However, I've always disliked it, but I never found anything better. I felt like it was just too verbose and seemed to get in my way more often than it helped. Finally I created Ouch, a minimalist exception handling system. Details after the jump.

Ouch is an exception handler that tries as much as possible to give you the power of something like Exception::Class, but with a minimalist interface. To throw an exception, you simply ouch:

ouch 404, 'File not found.', 'filename';

Nothing needs to be defined in advance. Catching exceptions is just as easy:

if (kiss 404) { do_something_with_the_exception($@); }

Ouch also has an object oriented interface to do some more advanced things:

my $e = eval { ouch $code, $message, $data } || Ouch->new($code, $message, $data); $e->trace; # full stack trace $e->scalar || scalar $e; # File not found at Uploader.pm line 45. $e->hashref; # { code => 404, message => 'File not found.', data => 'filename' } $e->code; # 404 $e->message; # File not found. $e->data; # filename

Ouch has a few other benefits. For one, since it uses exception codes rather than exception classes it is more friendly to work with in web interfaces, or anything else that uses status codes (HTTP, FTP, JSON-RPC, etc).

For another, it has a nice sugar function called bleep(), which removes the perlish stuff (stack trace, line numbers, etc) from an exception message (including non-Ouch exceptions). This can be incredibly useful if you want to display exception messages directly to an end user.

And finally, the data element is both optional and free form. You can throw any reference or scalar into it (so long as those references don't contain objects), which gives you some of the functionality of Exception::Class' "fields" parameter. I often find myself putting a field name into it to represent an exception thrown on a field where a user has entered inappropriate data. That makes it easy to highlight the field when I trap the exception.

In the module's documentation, I go into detail about why I think it's better than Exception::Class. What it really comes down to is that it's faster, smaller, has no dependencies, and costs a lot less typing. It's less than half that of Exception::Class.

Don't ge me wrong, I'm not saying Exception::Class is bad, or that you should ditch it. For what Exception::Class does, I've still found nothing better. However, if you're looking for good exception objects that are smaller and easier to use than Exception::Class, give Ouch a try. I've already switched a number of my projects over to it, and will be migrating all my CPAN modules in the coming weeks.