Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Catching DIE no matter what

by kosun (Acolyte)
on Aug 05, 2005 at 14:50 UTC ( [id://481265]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I have a CGI::Builder application that's dying in XML::Parser, and I want to catch it.

so I'm doing local $SIG{__DIE__} = sub { $var++; }; Which does indeed catch it, but after it hits that sub, it still dies with the XML::Parser error.

I've tried setting $SIG{__DIE__} globally with same problem :(

Is there some way to catch the die regardless? I don't want to have to do an eval because of the overhead in spawning another interpreter...

thank you! Kosun

Replies are listed 'Best First'.
Re: Catching DIE no matter what
by gellyfish (Monsignor) on Aug 05, 2005 at 15:00 UTC

    Unfortunately you can't prevent the program terminating like this - as described in perlvar:

    The routine indicated by $SIG{__DIE__} is called when a fatal exception is about to be thrown. The error message is passed as the first argument. When a __DIE__ hook routine returns, the exception processing continues as it would have in the absence of the hook, unless the hook routine itself exits via a "goto", a loop exit, or a die(). The "__DIE__" handler is explicitly disabled during the call, so that you can die from a "__DIE__" handler. Similarly for "__WARN__".
    If you want to catch this *and* not actually die then you need to use eval as well:
    local $SIG{__DIE__} = \&foo; + + eval { die "Aiieeee!"; }; + sub foo { print "COpped die"; }

    /J\

      kosun, as gellyfish's quote from perlvar says, you might be able to control things with a 'goto':
      $SIG{'__DIE__'} = sub { if ($_[0] =~ /matched error text/) { print('Warning - continuing after error: ', $_[0]); goto &foo; } else { print($_[0]); } }; sub foo { print("Continuing in 'foo'...\n"); # Do more work... }
      Your app will then exit when 'foo' returns.

      Remember: There's always one more bug.
Re: Catching DIE no matter what
by Joost (Canon) on Aug 05, 2005 at 15:06 UTC
    What's the error? Doesn't eval { ... } catch it?

    You should only use SIG{__DIE__} for terminating the process cleanly.

    update: eval doesn't spawn another interpreter. eval STRING compiles and executes the string, but that's not what you want either, you want eval BLOCK which just runs whatever code is in the block and catches exceptions.

    update2: see also eval

Re: Catching DIE no matter what
by broquaint (Abbot) on Aug 05, 2005 at 16:43 UTC
    In the case of XML::Parser it has its own __DIE__ handler which makes things a little tricky. But this is perl, the language where "never" doesn't really mean "never", it just means "probably shouldn't" ...
    BEGIN { ## Use CORE::die if you still want exceptions *CORE::GLOBAL::die = sub { warn "Problem? What problem? ... [@_]\n"; }; } use XML::Parser; XML::Parser->new->parse("This isn't XML. Seriously"); print "All is well\n"; __output__ Problem? What problem? [ syntax error at line 1, column 0, byte 0 at /usr/lib/perl5/XML/Parser. +pm line 187 ] All is well
    HTH

    _________
    broquaint

Re: Catching DIE no matter what
by halley (Prior) on Aug 05, 2005 at 16:04 UTC
    You may not be familiar with other languages like C++ or Java. However, in the off-chance that you are (or someone else reading this thread):
      die is to throw as
      eval { ... } is to try { ... }
    After an eval block, you can inspect $@ to see if the task died. As others have mentioned, $SIG{__DIE__} is only for putting in a hook for the last gasp of the process, when the die had no other eval blocks to catch it. By that point, there's no "saving" the process.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://481265]
Approved by tinita
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-18 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found