What is the best way to handle exceptions in Perl?

前端 未结 3 1388
忘掉有多难
忘掉有多难 2020-12-09 02:12

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 03:12

    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

提交回复
热议问题