NAME
Exceptions - handle exceptions in a standard OO-ish way
SYNOPSIS
use Exceptions;
try {
do_some_stuff();
die "error!" if $condition;
throw Exception msg => "Oops!" if $other_condition;
} catch FileException with {
print STDERR "File $E->{file} had a problem\n";
} catch IO::Exception with {
print STDERR $E->dump; # Structure dump of error
} except {
my $general_handler=sub {send_message $E->{description}};
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
}
Throw or catch a "prototype-mode" exception:
try {
throw HashException SUBCLASS => 'FeltLikeIt', specific => $variable;
opendir(...) or throw HashSysException SUBCLASS => 'OpenDir';
} catch HashSysException with {
print STDERR "Failed: $E->{ERRSTR}\n";
} catch HashException::FeltLikeIt with {
print STDERR "Well I didn't!\n";
}
DESCRIPTION
This module is still in an experimental stage. You know the basic routine: code in the try-block is executed, trapping errors; errors may be thrown inside the block (or any function it calls, of course), and they will be caught or not by catch-blocks according to the Perl inheritance hierarchy; otherwise-blocks handle all exceptions not otherwise caught; except-blocks should return a hash from class names to subroutine handlers, effectively creating part of the catch-class-with-block chain dynamically; finally-blocks are always supposed to be run no matter what, and hopefully they in fact do (they run after everything else, even if other blocks signal new errors).
EXCEPTION CLASSES
Exception objects can take any form. They need to be have a constructor which will be called normally from throw. They may implement dump (create a string representation) or print (print out some representation, currently unused).
Exception
The basic class. Takes optional msg and error_no initializers, filling in some basic context information. Other exception classes should prefereably inherit from this, even if every method is overridden.
SimpleException
"Thrown" by the die primitive. Just holds a message. Do not use yourself.
HashException
Convenient superclass for prototyping purposes, before you have a rigidly-worked-out exception hierarchy (if you ever do). Takes any key-value initializers, and can display them. (Data::Dumper is preferred for this.) Prepopulates some useful info from the call stack for you. The special key SUBCLASS causes the actual exception-class thrown to be the named subclass (and subpackage) of the master throwing class (here, HashException, but also its children). catch can handle catching such a "prototype" class, even if it has never been defined or thrown before. Obviously this is not great for long-term organization but it can tide you over.
HashSysException
Just like HashException, but includes info from $!
, so it is useful for throwing exceptions related to system-level operations.
BUGS
There probably are some.
AUTHORS
Peter Seibel <peter@weblogic.com>, originally. Adopted by Jesse Glick <jglick@sig.bsh.com>.
REVISION
Exceptions/lib/Exceptions.pm last modified Sat, 20 Sep 1997 19:46:47 -0400 release 0.002. Copyright (c) 1997 Strategic Interactive Group. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.