
Exception::Caught - Sugar for class-based exception handlers

version 0.01

use Try::Tiny;
use Exception::Caught;
use Exception::Class qw(MyException);
try {
MyException->throw();
}
catch {
rethrow unless caught('MyException');
# do something special with $_
};

Doing different things with an exception based on its class is a common pattern. TryCatch does a good job with this, but is a bit heavyweight. Try::Tiny is nice and lightweight, but doesn't help out with the type dispatching problem. Hence, this module. You don't have to use it with Try::Tiny, but that's what it's best at.

Exception::Caught uses Sub::Exporter, so see that module if you want to rename these subroutines to something else. The subs are only exported for your lexical scope so that you don't get extra methods/subs in your package/class. If this isn't what you want, consider using Sub::Import.
caught and rethrow are exported by default.
Returns true if the exception (optional argument, defaults to $_) passes isa() for the given classname.
Calls rethrow on the exception (optional argument, defaults to $_) if it is an object with a rethrow method, otherwise just dies with the exception.

Paul Driver <frodwith@cpan.org>

This software is copyright (c) 2010 by Paul Driver <frodwith@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.