Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Error Handling Misconception

by kesterkester (Hermit)
on Sep 04, 2003 at 13:04 UTC ( #288853=note: print w/replies, xml ) Need Help??


in reply to Error Handling Misconception

I'm interested in what advice the monks can give on the following form of eval error catching-- I picked it up at work and have been using it for a while, with pretty good success. I use programs that run other programs at work, and this displays which program died when a die happens.
eval { main () }; print STDERR "# $0: ", $@ and exit 1 if $@; exit 0; sub main { # do everything ... }
I don't use the try/catch form of eval much, although it does seem to work with the above:
eval { main () }; print STDERR "# $0: ", $@ and exit 1 if $@; exit 0; sub main { my $val = eval { test_sub () }; print "hi" if $@; } sub test_sub { die "test_sub dies" }
prints "hi", not "# <programname>: test_sub dies", as I expected. Any advice? Could this lead me to shoot myself in the foot later on? Thanks much.

Replies are listed 'Best First'.
Re: Re: Error Handling Misconception
by bobn (Chaplain) on Sep 06, 2003 at 14:52 UTC

    Why did you expect it to do anything else? Your inner eval caught the first exception, but instead of propagating it, you discarded it and printed "hi". The outer eval saw no error, becauase the iner eval ignored it.

    This program behaved exactly as expected.

    If you wanted the this to do something else, the followingin (untested) would have been better:

    sub main { my $val = eval { test_sub () }; die "hi" if $@; }

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://288853]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2020-05-25 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (146 votes). Check out past polls.

    Notices?