Exception::Simple - simple exception class
use Exception::Simple; use Try::Tiny; #or just use eval {}, it's all good ### throw ### try{ Exception::Simple->throw( 'oh noes!' ); } catch { warn $_; #"oh noes!" warn $_->error; #"oh noes!" }; my $data = { 'foo' => 'bar', 'fibble' => [qw/wibble bibble/], }; try{ Exception::Simple->throw( 'error' => 'oh noes!', 'data' => $data, ); } catch { warn $_; #"oh noes!" warn $_->error; #"oh noes!" warn $_->data->{'foo'}; #"bar" }; ### throwc ### try{ Exception::Simple->throwc( "Some::Fake::Exception::Class", 'oh noes!' ); } catch { warn $_; #"oh noes!" warn ref( $_ ); #"Some::Fake::Exception::Class" warn $_->error; #"oh noes!" }; my $data = { 'foo' => 'bar', 'fibble' => [qw/wibble bibble/], }; try{ Exception::Simple->throwc( 'Some::Fake::Exception::Class', 'error' => 'oh noes!', 'data' => $data, ); } catch { warn $_; #"oh noes!" warn ref( $_ ); #"Some::Fake::Exception::Class" warn $_->error; #"oh noes!" warn $_->data->{'foo'}; #"bar" };
pretty simple exception class. auto creates argument accessors. simple, lightweight and extensible are this modules goals.
with just one argument $@->error is set Exception::Simple->throw( 'error message' ); # $@ stringifies to $@->error
or set multiple arguments (creates accessors) Exception::Simple->throw( error => 'error message', data => 'cutom atrribute', ); # warn $@->data or something
Same as throw, except the first argument is a classname which will become the classname of the exception object.
Useful for throwing specific execptions without having to create lots of packages.
try{ Exception::Simple->throwc( "Some::Fake::Exception::Class", 'oh noes!' ); } catch { warn ref( $_ ); #Some::Fake::Exception::Class };
WARNING: using throwc with a real classname is unsupported i.e. throwc( "Data::Dumper", "derp" ) #you just made god kill a kitten
say you catch an error, but then you want to uncatch it
use Try::Tiny; try{ Exception:Simple->throw( 'foobar' ); } catch { if ( $_ eq 'foobar' ){ #not our error, rethrow $_->rethrow; } };
accessor for error, if its been set
Bugs should always be submitted via the CPAN bug tracker
For other issues, contact the maintainer
n0body <n0body@thisaintnews.com>
Stephen Thirlwall
http://thisaintnews.com, Try::Tiny
Copyright (C) 2012 by n0body http://thisaintnews.com/
This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
To install Exception::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Exception::Simple
CPAN shell
perl -MCPAN -e shell install Exception::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.