How to properly use the try catch in perl that error.pm provides?

前端 未结 4 723
南旧
南旧 2021-02-03 20:04

I have found that there is the module Error that provides try and catch functionality like in java. But I am confused at how you can print the exception that returns.

I

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-03 20:22

    Unfortunately TryCatch has been broken with the new version 0.006020 of Devel::Declare and there seems to be no intention of fixing it. The perl core developers team also complained about the funky stuff TryCatch was relying on to make it work.

    Instead there is a new implementation called Nice::Try, which is a perfect replacement.

    There is no need to have semi colon on the last brace like Try::Tiny.

    You can also do exception variable assignment like

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

    It also works using class exception like

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

    And it also supports finally. Its features set make it quite unique.

    Full disclosure: I have developed this module when TryCatch got broken.

提交回复
热议问题