3

How do I ignore a die() that occurs in a Perl END block?

As it is now I get

END failed--call queue aborted

And the error bubbles up the calling script.

3 Answers 3

8

Put your end block inside an eval { .... } - this should prevent the behaviour you describe.

#!/usr/bin/perl print("hi there!\n"); END { eval{ print("goodbye\n"); die("this can't hurt...."); }; #detect if an error occurred in the eval if ( $@ ){ print ("an error occurred: $@\n"); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Great, this does the trick. Doesn't seem to work for "exit(..)" inside an eval though?
@OneSolitaryNoob You cannot trap exit via eval.
5

Place your code inside eval block and if you want to retrieve the error message that die provides you can capture using if condition outside the block.

#!/usr/bin/perl my $val=1; eval { print "Inside Eval\n"; if($val == 1) { die "hello world\n"; } print "End of Eval\n"; }; if ( $@ ) { print "Error message - $@" . "\n"; } 

1 Comment

+1 well done for showing how to pick up the error - have added that to my answer too for completeness.
3

Try::Tiny is an excellent wrapper for eval, allowing you to explicitly handle run-time exceptions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.