in reply to Exception handling (alternatives)

Frankly, based on a number of searches that I'd done on CPAN there were only a handful of somewhat similar packages, however, they didn't meet my needs... The purpose for thise effort, therefore, may be to find another 'alternative' approach at error handling.

Can you explain a little about how your approach differs from Error? At first glance, I don't see much difference.

Replies are listed 'Best First'.
Re: Re: Exception handling (alternatives)
by vladb (Vicar) on Dec 21, 2001 at 21:53 UTC
    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
      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.