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

  1. Do you use exception objects? If so, which one and why? If not, why not?
  2. If you don't use exception objects, what would entice you to use them?
  3. If you do use exception objects, what do you hate about them, and what could be better?
  4. Is objectifying exceptions in the DIE handler a bad idea?
  5. Where should I objectify my exceptions? In my eval{} wrapper? In a sigdie handler?
  6. Are there any papers, articles or other resources on exceptions in general and in Perl that you find useful or enlightening.


TGI says moo


In reply to Do you use an exception class in your Perl programs? Why or why not? by TGI

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.