Skip to content

Instantly share code, notes, and snippets.

@koorchik
Created May 19, 2012 08:02
Show Gist options
  • Select an option

  • Save koorchik/2729994 to your computer and use it in GitHub Desktop.

Select an option

Save koorchik/2729994 to your computer and use it in GitHub Desktop.
Try::Tiny with helper
# ...
test_exception( Exception1->new() );
test_exception( Exception2->new() );
test_exception( Exception3->new() );
test_exception("Special text\n");
test_exception("Text exception\n");
use Scalar::Util qw/blessed/;
sub e($) {
my $class = shift;
my $e = $_;
return unless blessed($e);
return $e->isa($class);
}
sub test_exception {
my $e = shift;
try {
die $e;
}
catch {
when ( e 'Exception1') {
say ref($_) . " matches Exception1";
}
when ( e 'Exception2' ) {
say ref($_) . " matches Exception2";
}
when ( e 'Exception::Base' ) {
say ref($_) . " matches Exception::Base";
}
when (/Special/) {
chomp;
say qq{"$_" matches qr/Special/};
}
default {
chomp;
say qq{"$_" matches default};
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment