4 # Here's a little code I use for exception handling. It's really just
5 # glorfied eval/die. The way to use use it is when you might otherwise
6 # exit, use &throw to raise an exception. The first enclosing &catch
7 # handler looks at the exception and decides whether it can catch this kind
8 # (catch takes a list of regexps to catch), and if so, it returns the one it
9 # caught. If it *can't* catch it, then it will reraise the exception
10 # for someone else to possibly see, or to die otherwise.
12 # I use oddly named variables in order to make darn sure I don't conflict
13 # with my caller. I also hide in my own package, and eval the code in his.
15 # The EXCEPTION: prefix is so you can tell whether it's a user-raised
16 # exception or a perl-raised one (eval error).
21 # if (&catch('/$user_input/', 'regexp', 'syntax error') {
22 # warn "oops try again";
26 # if ($error = &catch('&subroutine()')) { # catches anything
28 # &throw('bad input') if /^$/;
32 local($__code__, @__exceptions__) = @_;
33 local($__package__) = caller;
34 local($__exception__);
36 eval "package $__package__; $__code__";
37 if ($__exception__ = &'thrown) {
38 for (@__exceptions__) {
39 return $__exception__ if /$__exception__/;
41 &'throw($__exception__);
46 local($exception) = @_;
47 die "EXCEPTION: $exception\n";
51 $@ =~ /^(EXCEPTION: )+(.+)/ && $2;