Resumable Exceptions (sort of)

chromatic on 2005-11-06T22:20:20

Sometimes it's nice to have a single exception block guarding several potentially fatal operations. This lets you re-use error-handling code. However, sometimes you can recover from a single exception and move on to the next operation. Unfortunately, Perl doesn't really support this.

That's why I wrote some very simple code. Here's how it looks like from the Perl side:

#!/usr/bin/perl

use strict;
use warnings;

use Runops::Resume;

my $text;
eval
{
    $text = 'before';
    print $text, "\n";
    $text = 'after';
    die "Goodbye!\n";
    print $text, "\n";
};

warn "Died '$@'" if $@;
resume();

As you ought to expect, the output is:

before
Died 'Goodbye!
' at example.pl line 17.
after

Note that declaring lexicals within the eval block doesn't quite work correctly. That would be scary code.

Between Perl and C, it's fewer than 70 lines of well-spaced code. It's reasonably trivial to make a stack of resumable exceptions, too -- probably fewer than ten more lines of C. Runops::Resume.


Stacks, multiplicity

jjore on 2007-07-23T02:57:08

You're getting very close to wanting mess with the stacks and serialize/marshal them. If the die wasn't within the current frame then I imagine stuff would be all fu-bar...

except that I also see you're doing another goto() to immediately following the resume() on falling out of the eval{}. Hmm. If the eval does some kind of further exceptional flow control with die/next/goto perhaps that's a problem.

Your additional runloop has me wishing these modules were implemented as advice to another runloop. Consider having before/around/after advice in this space. Then our various runloops might be able to co-exist.