The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Exception::Simple - simple exception class

SYNOPSIS

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! at filename.pl line 3"
    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"
};

DESCRIPTION

pretty simple exception class. auto creates argument accessors. simple, lightweight and extensible are this modules goals.

METHODS

throw

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

rethrow

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; 
    }
};

error

accessor for error message (set if only 1 arg is passed to throw)

package

package that threw the exception

filename

filename of the code that threw the exception

line

line number that threw the exception

CAVEATS

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

SUPPORT

Please submit bugs through https://github.com/n0body-/exception-simple/issues

For other issues, contact the maintainer

AUTHOR

n0body <n0body@thisaintnews.com>

CONTRIBUTORS

Stephen Thirlwall

SEE ALSO

http://thisaintnews.com, Try::Tiny

LICENSE

Copyright (C) 2013 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.