Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Most of our code uses Error::Simple subclasses (Error objects henceforth) to throw and/or catch thrown exceptions as Error objects. In some portions of the code, exceptions are thrown with &Carp::croak (or occassionaly with &die).

Should the croak's be handled via eval{}-$@, or via try-catch even though croak's are not Error objects? Put it other way, would there be problem in using Error objects to capture plain croak (or, die)? (perl is 5.8.[78].)

Replies are listed 'Best First'.
Re: Error::Simple or eval{} use to capture &Carp::croak
by bluestar (Novice) on Sep 03, 2009 at 13:06 UTC
    Sounds like you need to override die and/or croak. *CORE::GLOBAL::die = sub { ... }
    Even better, you could use use ex::override

    Be very careful though :)
      I do not want to override die|croak; I want just to be able to do something after they have done their deed, which eval-$@ handles. So could try-catch, but I am unsure about potential problems.
        I think the error from croak is sent to warn so maybe you can do something after that error has accrued like this.
        $SIG{__WARN__} = sub { my $wn = shift; handle_this_error() if $wn =~ /The_Error_Your_After/i; warn $wn; };


Re: Error::Simple or eval{} use to capture &Carp::croak
by SFLEX (Chaplain) on Sep 04, 2009 at 10:44 UTC
    croak - die of errors (from perspective of caller)
    croak is handled using eval{} and error is found by $@.
    I have no idea what you mean by "Error objects" but to give you a better idea of how Carp works, take a look at the source.

    What did the Pro-Perl programmer say to the Perl noob?
    You owe me some hair.

      Written earlier, emphasis added ...

      ... Error::Simple subclasses (Error objects henceforth) ...

      ... as in ...

      # In ModErr.pm ... package ModErr; use base 'Error::Simple'; 1; # Elsewhere ... use ModErr qw[ :try ]; try { 1 == int( rand(2) ) ? call_me() : wont_you_call() ; } catch ModErr with { my ( $err ) = @_; my $text = $err->text; throw ModErr "No call back." unless $text =~ /your call/i; }; print "Everything is ok after all!\n"; sub call_me { throw ModErr "Here is your call!"; } sub wont_you_call { throw ModErr 'please?'; }