What is the best way to handle exceptions in Perl?

33,662

Solution 1

The consensus of the Perl community seems to be that Try::Tiny is the preferred way of doing exception handling. The "lenient policy" you refer to is probably due to a combination of:

  • Perl not being a fully object-oriented language. (e.g. in contrast to Java where you can't avoid dealing with exceptions.)
  • The background of many Perl developers. (Languages like C1 and shell don't have exception mechanisms.)
  • The kind of tasks people tend to use Perl for. (Small scripts for text munging and report generation where exception handling isn't needed.)
  • Perl not having a (good) built-in exception mechanism.

Note that the last item means that you'll see a lot of code like this:

eval { something() };
if ($@) {
    warn "Oh no! [$@]\n";
}

That's exception handling even though it doesn't use try/catch syntax. It's fragile, though, and will break in a number of subtle edge cases that most people don't think about. Try::Tiny and the other exception handling modules on CPAN were written to make it easier to get right.

1. C does have setjmp() and longjmp(), which can be used for a very crude form of exception handling.

Solution 2

As it has been mentioned you can use the traditional way with eval, but if you want to use more elaborate exception trapping, including with exception objects, then I recommend using the try-catch-finally blocks. There are quite a few perl modules that provide it such as Nice::Try and Syntax::Keyword::Try, but Syntax::Keyword::Try does not provide exception variable assignment or exception class catch like

  try
  {
    # something
  }
  catch( Exception $e )
  {
    # catch this in $e
  }

Full disclosure: I am the developer of Nice::Try

Share:
33,662
ennuikiller
Author by

ennuikiller

29,200 ? I can calculate the motion of heavenly bodies but not the madness of people -- Sir Isaac Newton Time heals all wounds, but ultimately kills the organism.... -- paraphrased Hector Berlioz quote "Lloyd-Webber's awful stuff Runs for years and years and years An earthquake hits the theatre But the operetta lingers Then the piano lid comes down And breaks his fucking fingers It's a miracle" - Roger Waters What God Wants God Gets Succinctness is Godliness oh and just in case anyone is wondering: ennui - interminable boredom killer - a person or thing that kills ennuikiller - a person who kills interminable boredom About the avatar: It is in memory of 2 of the greatest geniuses of the 20th century (or perhaps in any century). 1. Peter Sellers whose comic genius inspired a whole slew of successful comedians and what better accomplishment to leave to the world than laughter!! 2.Stanley Kubrick whose films have been both iconoclastic and profoundly humanistic (I think some may have missed this point). I believe his crowning achievement is in fact Dr. Strangelove who is pictured in the avatar. A dark comedy (but with very serious overtones) about the ease with which the human species can eradicate itself. If you haven't seen it, do yourself a favor and watch it RIGHT NOW!

Updated on June 02, 2020

Comments

  • ennuikiller
    ennuikiller almost 4 years

    I've noticed that Exception.pm and Error.pm don't seem to be extensively used in the Perl community. Is that due to the large footprint of eval for exception handling?

    Also Perl programs appear to have a much more lenient policy regarding exception handling in general. Is there a compelling reason for this?

    In any event what would be the best method for exception handling in Perl?