Hint on local DIE handlers in Eval

David Dick david_dick at iprimus.com.au
Thu Jan 9 23:41:24 CST 2003


>
>
>Using a die handler is useful when the regular behavior of die
>is not acceptable.  An example might be when you want a fatal
>error to perform a special action (SMS some poor programmer),
>or when you're writing a CGI script and want fatal errors to
>go someplace other than the regular error log.
>  
>

ok, coherence probably is not my long suit today... :) I'm just not sure 
I understand the difference between

a)

{
    local SIG{'__DIE__'} = &handle_error(@_);
    die "Whoops";
}

and

b)

eval {
    die "Whoops";
};
if ($@) {
       &handle_error($@);
}

*praying* that the code above is vaguely correct, the two structures 
seem to have on the surface at least, quite different ways of going 
after the problem of catching the exception.  I was just wondering if 
there was an advantage (beyond situational "it felt right/wanted to 
upset the maintenance coder" sort of thing) to using one technique or 
the other.  As you and Scott have pointed out, use a DIE handler seems 
to be a downright scary way of handling exceptions, is there _any_ 
advantage to it at all? Should exterminating this structure where-ever 
possible be regarded as a mercy killing? :)

Cheers
-dave

>A die handler may also have use in performing clean-up, such as
>closing connections or rolling back transactions in progress.
>However, as pointed out by Scott, just because you're in a die
>handler doesn't mean your program is going to halt anytime soon.
>I'd *strongly* recommend the use of END {} blocks to perform any
>final clean-up you want done on program exit.
>
>One of the big problems with die handlers is that people don't
>expect them to be changed, hence the need for the rather
>careful footwork seen in Scott's examples.  I'd recommend not
>touching die handlers at all if you can help it, and if you
>*do* need to play with them, restrict them to logging and/or
>modifying the message in question (eg, to wrap it up in an
>exception object), and then re-throwing the die.
>
>Cheers,
>
>	Paul
>
>- -- 
>Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
>Director of Training                   | Ph:  +61 3 9354 6001
>Perl Training Australia                | Fax: +61 3 9354 2681
>-----BEGIN PGP SIGNATURE-----
>Version: GnuPG v1.0.6 (GNU/Linux)
>Comment: For info see http://www.gnupg.org
>
>iD8DBQE+Hlncx5N6j7FHnlURAkB9AJkBBJEW/bREOp7Yt//b+tFitzxMgACfZHKu
>g3niS1n8iZeRjq8Rh7tJFmc=
>=gud0
>-----END PGP SIGNATURE-----
>
>  
>




More information about the Melbourne-pm mailing list