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" };
pretty simple exception class. auto creates argument accessors. simple, lightweight and extensible are this modules goals.
When using this module, you can specify a shortcut method, so you don't have to type the full module name each time.
This works by importing a sub with the name specified into the current namespace, that returns the package name so you need to make sure this sub does not already exist, or you'll get an error
e.g.
use Exception::Simple qw/E/; use Try::Tiny; #or just use eval {}, it's all good ### throw ### try{ E->throw( 'oh noes!' ); } catch { warn ref $_; # Exception::Simple warn $_; #"oh noes!" warn $_->error; #"oh noes!" };
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 => 'custom attribute', ); # warn $@->data or something
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 message (set if only 1 arg is passed to throw)
package that threw the exception
filename of the code that threw the exception
line number that threw the exception
If you pass in package, filename or line, they will be overwritten with the caller information
If you don't pass in error, then you'll get an undef warning on stringify
Please submit bugs through https://github.com/markwellis/exception-simple/issues
For other issues, contact the maintainer
Mark Ellis <markellis@cpan.org>
Stephen Thirlwall
Try::Tiny aliased
Copyright 2014 Mark Ellis <markellis@cpan.org>
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.