How to handle both caught and unprocessed errors in a Perl routine?

It continued , "How can I get around" the call of death "in the Perl library that I can not change? .

I have a routine that causes a library to crash - sometimes many times. Instead of eating every call inside this routine with eval {}, I just let it die and use eval {} at the level that calls my routine:

my $status=eval{function($param);};
unless($status){print $@; next;}; # print error and go to
                                  # next file if function() fails

However, there are errors that I can and can catch in the function (). What is the most correct / elegant way to develop errors in a subroutine and calling procedure so that I get the correct behavior for both caught and uncaught errors?

+5
source share
2 answers

The eval block can be nested:

sub function {
    eval {
        die "error that can be handled\n";
        1;
    } or do {
        #propagate the error if it isn't the one we expect
        die $@ unless $@ eq "error that can be handled\n"; 
        #handle the error
    };
    die "uncaught error";
}

eval { function(); 1 } or do {
    warn "caught error $@";
};
+8
source

I'm not quite sure what you want to do, but I think you can do it with a handler.

$SIG{__DIE__} = sub { print $@ } ;

eval{ function($param); 1 } or next;
0
source

All Articles