in reply to Re: Exception handling (alternatives)
in thread Exception handling (alternatives)

It definitely is a very decent package. However, it doesn't seem to handle plain die() correctly (or at least the way I expect it to)
try { die "foobar"; } catch { print "failed\n"; exit; } print "ok";
After running this code (having included Error.pm etc.), I do see the "failed" string, however, I can't seem to be able to see the die string ("foobar"). Here's what the package dump (via perldb "V Error" command) looks inside catch {} clause:
$("" = 'stringify' $(0+ = 'value' $Debug = 1 $VERSION = 0.15 $Depth = 0 $() = 1 %OVERLOAD = ( 'dummy' => 1 )
Clearly, the Error package doesn't seem to have a record of the original die string. For my intents and purposes I need just that since I have to wrap my exception handling code around older code that relies on eval/die as it's exception handling mechanism.

Despite of this, there does seem to be a (lengthy) workaround this problem:
try { eval { die "foobar"; }; if ($@) { throw Error::Bad($@); } } catch Error::Bad with { my $err = shift; print "Failed: $err\n"; exit; } print "ok";
However, even this code doesn't work for some reason =/. Here's what I get:
okCan't use string ("1") as a HASH ref while "strict refs" in use at E +rror.pm line 183.
I would be greatful if someone could explain me how this should be made to work ;-).

Thank you for pointing to Error.pm, however. Looking at it as is, I think I could simply modify it a bit here and there to fit my needs (and hopefully make it useful to others who are in a like position).

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Replies are listed 'Best First'.
Re: Re: Re: Exception handling (alternatives)
by perrin (Chancellor) on Dec 21, 2001 at 22:23 UTC
    You can catch normal die stuff with Error. I used it to catch DBI errors with the RaiseError flag on.

    try { die "foobar"; } catch Error with { my $error = shift; print "failed: $error\n"; exit; }; print "ok";

    Make sure you don't leave off the last semi-colon after the catch block, or it won't work.