use Error qw(:try); throw Error::Simple( "A simple error"); sub xyz { ... record Error::Simple("A simple error") and return; } unlink($file) or throw Error::Simple("$file: $!",$!); try { do_some_stuff(); die "error!" if $condition; throw Error::Simple "Oops!" if $other_condition; } catch Error::IO with { my $E = shift; print STDERR "File ", $E->{'-file'}, " had a problem\n"; } except { my $E = shift; my $general_handler=sub {send_message $E->{-description}}; return { UserException1 => $general_handler, UserException2 => $general_handler }; } otherwise { print STDERR "Well I don't know what to say\n"; } finally { close_the_garage_door_already(); # Should be reliable }; # Don't forget the trailing ; or you might be surprised
Errors in the class "Error" should not be thrown directly, but the user should throw errors from a sub-class of "Error".
The BLOCK will be evaluated and, if no error is throw, try will return the result of the block.
"CLAUSES" are the subroutines below, which describe what to do in the event of an error being thrown within BLOCK.
"BLOCK" will be passed two arguments. The first will be the error being thrown. The second is a reference to a scalar variable. If this variable is set by the catch block then, on return from the catch block, try will continue processing as if the catch block was never found. The error will also be available in $@.
To propagate the error the catch block may call "$err->throw"
If the scalar reference by the second argument is not set, and the error is not thrown. Then the current try block will return with the result from the catch block.
When evaluated "BLOCK" will be passed one argument, which will be the error being processed. The error will also be available in $@.
Only one otherwise block may be specified per try block
If the handler throws an error then the error will be caught, the finally block will be executed and the error will be re-thrown.
Only one finally block may be specified per try block
-file -line -text -value -object
If "-file" or "-line" are not specified in the constructor arguments then these will be initialized with the file name and line number where the constructor was called from.
If the error is associated with an object then the object should be passed as the "-object" argument. This will allow the "Error" package to associate the error with the object.
The "Error" package remembers the last error created, and also the last error associated with a package. This could either be the last error created by a sub in that package, or the last error which passed an object blessed into that package as the "-object" argument.
"throw" may also be called on an existing error to re-throw it.
die with Some::Error ( ... );
record Some::Error ( ... ) and return;
$Error->flush;
$ber->encode(...) or return Error->prior($ber)->associate($ldap);
By default this method returns the "-text" argument that was passed to the constructor, or the string "Died" if none was given.
By default this method returns the "-value" argument that was passed to the constructor.
If the text value ends with "at file line 1" as $@ strings do, then this infomation will be used to set the "-file" and "-line" arguments of the error object.
This class is used internally if an eval'd block die's with an error that is a plain string. (Unless $Error::ObjectifyCallback is modified)
It accepts a single argument which is a hash reference to named parameters. Currently the only named parameter passed is 'text' which is the text of the error, but others may be available in the future.
For example the following code will cause Error.pm to throw objects of the class MyError::Bar by default:
sub throw_MyError_Bar { my $args = shift; my $err = MyError::Bar->new(); $err->{'MyBarText'} = $args->{'text'}; return $err; } { local $Error::ObjectifyCallback = \&throw_MyError_Bar; # Error handling here. }
use Error qw( :warndie );
These new error handlers are installed in $SIG{__WARN__} and $SIG{__DIE__}. If these handlers are already defined when the tag is imported, the old values are stored, and used during the new code. Thus, to arrange for custom handling of warnings and errors, you will need to perform something like the following:
BEGIN { $SIG{__WARN__} = sub { print STDERR "My special warning handler: $_[0]" }; } use Error qw( :warndie );
Note that setting $SIG{__WARN__} after the ":warndie" tag has been imported will overwrite the handler that "Error" provides. If this cannot be avoided, then the tag can be explicitly "import"ed later
use Error; $SIG{__WARN__} = ...; import Error qw( :warndie );
Can't call method "foo" on an undefined value at examples/warndie.pl line 16.
into
Unhandled perl error caught at toplevel: Can't call method "foo" on an undefined value Thrown from: examples/warndie.pl:16 Full stack trace: main::inner('undef') called at examples/warndie.pl line 20 main::outer('undef') called at examples/warndie.pl line 23
Error::Exception aims to combine Error and Exception::Class ``with correct stringification''.
The code that inspired me to write this was originally written by Peter Seibel <peter@weblogic.com> and adapted by Jesse Glick <jglick@sig.bsh.com>.
":warndie" handlers added by Paul Evans <leonerd@leonerd.org.uk>