游.程 2012-04-28 15:34 采纳率: 0%
浏览 423
已采纳

是 Try: : Tiny 仍然被推荐用于佩尔5.14或更高版本的异常处理?

The consensus of the Perl community seems to be that Try::Tiny is the preferred way to handle exceptions.

Perl 5.14 (which is the version I use) seems to solve the issues with eval that Try::Tiny addresses. Will Try::Tiny still provide any benefits for me?

转载于:https://stackoverflow.com/questions/10364975/is-trytiny-still-recommended-for-exception-handling-in-perl-5-14-or-later

  • 写回答

4条回答 默认 最新

  • 狐狸.fox 2012-04-29 17:28
    关注

    My answer is unpopular, but I don't think Perl programmers should be trying to use the exceedingly poor notion of the thing we call "exceptions" in Perl. These are essentially a side channel return value. However, still being enamored with the idea of exceptions, even with all the complexities of using a global variable to pass around state, people keep trying to make it work.

    Practically, however, people use die to signal failure. Some will say that you can die with a reference and pass back error objects, but you don't need die for that. We have objects, so we should use all the power of objects:

     sub some_sub {
        ...
        return Result->new( error => 1, description => ... ) if $something_went_wrong;
        return Result->new( error => 0, ... );
        }
    
     my $result = some_sub( ... );
     if( $result->is_error ) { ... };
    

    That doesn't involve global variables, action at a distance, scoping headaches, or require special specials. You create a tiny class Result, or whatever you want to call it, to wrap your return values so you have structured data instead of single values with no identity. There's no more wondering what a return value means. Is that undef a real value or an indication of failure? Is the return value good if it's defined or if it's true? Your object can tell you these things. And, you can use the same object with die. If you're already using the object with die and using it as the return value, there's very little to recommend all the extra stuff you have to do to tolerate $@.

    I talk more about this in "Return error objects instead of throwing exceptions"

    However, I know that you can't help what other people do, so you still have to pretend Perl has exceptions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置