-
In the process of learning perl a while back, and after programming in Python for a year, I found the eval block method that perl uses inelegant. I eventually got used to the standard way of doing exceptions in perl, but at the time I was impressed by the following snippet of code from the perlsub manpage:
sub try (&@) { my($try,$catch) = @_; eval { &$try }; if ($@) { local $_ = $@; &$catch; } } sub catch (&) { $_[0] } try { die "phooey"; } catch { /phooey/ and print "unphooey\n"; };
"That's neat!", I thought to myself. So, I decided to play around with it to understand how it worked, and to see if I could improve it. The result was I dreamt up a rather bizarre yet (IMHO) interesting variation on handling exceptions. The "interesting" part is that the exception handler lets you continue within the block that threw the exception after handling it.

Exception.pm:

package Exception; use strict; our ($try, $catch, $prior_catch); BEGIN { use Exporter; our ($VERSION, @ISA, @EXPORT); $VERSION = "0.04.5"; @ISA = qw(Exporter); @EXPORT = qw(&try &catch); *CORE::GLOBAL::die = \¨ # this really should probably be d +one another # way or at least be optional. } sub die { my $catch_sub = $catch; local $catch = $prior_catch; local $_ = shift; chomp; (defined($catch_sub) && $catch_sub ne "" && &$catch_sub) || CO +RE::die $_; } sub catch (&) { } sub try (&@) { local $prior_catch = $catch; { local ($try, $catch) = @_; eval { &$try; } } &die($@) if $@; } return 1;
exceptiondemo.pl:
#!/usr/bin/perl use strict; use Exception; sub throw { my $err = shift; print "throwing $err\n"; die $err; } catch { die "You shouldn't see this.\n"; }; # This would simply be ins +ane try { throw "foo"; try { throw "bar"; throw "baz"; print "You should NOT see this line, baz was fatal to +this block.\n"; } catch { /^bar/ and do { print "$_ caught and trapped\n"; return 1; # one means exception is "trapped". # unlike using eval, the execution c +ontinues # in the block that threw the except +ion }; /^baz/ and do { print "$_ caught, but we won't trap it here\n" +; return 0; # zero means "untrapped". Even thoug +h we # can do some processing here, we'll + propagate # the exception "outward" }; }; throw "quux"; print "The outer try block completed successfully\n"; } catch { /^foo/ and do { print "$_ caught\n"; return 1; }; /^baz/ and do { print "Caught $_ in outer catch, we'll trap it now\n"; return 1; }; /^quux/ and do { print "$_ caught\n"; try { throw "quuux within quux's catch!"; } catch { /^quuux/ and do { print "$_ caught.\n"; return 1; }; }; return 1; }; }; print "\n"; print "Done testing logic!\n";
The code has never been used for anything more than a toy, so be warned that you shouldn't drop this into your latest project and ship it out:P

In reply to exception handling (the weird way) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.