Hint on local DIE handlers in Eval

Paul Fenwick pjf at perltraining.com.au
Thu Jan 9 23:27:56 CST 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

G'day David,

On Fri, Jan 10, 2003 at 03:30:26PM +1100, David Dick wrote:

> >i've never actually used a DIE handler before.  Why would you use 
> >them?  I understand that intercepting a exception thrown by a die and 
> >informing the user about it in a graceful fashion is a useful 
> >thing(tm), but i tend to use an eval block (or similar such as 
> >Error.pm) around the suspect code and a test afterwards.  Is the DIE 
> >handler just another example of TIMTOWTDI, or does it have specific 
> >advantages/disadvantages (not merely gotchas) associated with it?
> >-dave

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.

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