Cross posted on Stack Overflow
I've got a bunch of questions about how people use exceptions in Perl. I've included some background notes on exceptions, skip this if you want, but please take a moment to read the questions and respond to them.
Thanks.
Background on Perl Exceptions
Perl has a very basic built-in exception system that provides a spring-board for more sophisticated usage.
For example die "I ate a bug.\n"; throws an exception with a string assigned to $@.
You can also throw an object, instead of a string: die BadBug->new('I ate a bug.');
You can even install a signal handler to catch the SIGDIE psuedo-signal. Here's a handler that rethrows exceptions as objects if they aren't already.
$SIG{__DIE__} = sub { my $e = shift; $e = ExceptionObject->new( $e ) unless blessed $e; die $e; }
This pattern is used in a number of CPAN modules. but perlvar says:
Due to an implementation glitch, the $SIG{DIE} hook is called even inside an eval(). Do not use this to rewrite a pending exception in $@ , or as a bizarre substitute for overriding CORE::GLOBAL::die() . This strange action at a distance may be fixed in a future release so that $SIG{DIE} is only called if your program is about to exit, as was the original intent. Any other use is deprecated.
So now I wonder if objectifying exceptions in sigdie is evil.
The Questions
- Do you use exception objects? If so, which one and why? If not, why not?
- If you don't use exception objects, what would entice you to use them?
- If you do use exception objects, what do you hate about them, and what could be better?
- Is objectifying exceptions in the DIE handler a bad idea?
- Where should I objectify my exceptions? In my eval{} wrapper? In a sigdie handler?
- Are there any papers, articles or other resources on exceptions in general and in Perl that you find useful or enlightening.
TGI says moo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Do you use an exception class in your Perl programs? Why or why not?
by zwon (Abbot) on Mar 13, 2010 at 21:53 UTC | |
by TGI (Parson) on Mar 14, 2010 at 01:39 UTC | |
by zwon (Abbot) on Mar 14, 2010 at 10:14 UTC | |
Re: Do you use an exception class in your Perl programs? Why or why not?
by creamygoodness (Curate) on Mar 15, 2010 at 00:03 UTC | |
by TGI (Parson) on Mar 15, 2010 at 04:59 UTC | |
Re: Do you use an exception class in your Perl programs? Why or why not?
by doug (Pilgrim) on Mar 14, 2010 at 23:22 UTC |