加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

异常 – 为什么Perl 6尝试不在shell()中处理非零退出?

发布时间:2020-12-15 23:36:49 所属栏目:大数据 来源:网络整理
导读:这 try捕获异常: try die X::AdHoc;say "Got to the end"; 输出显示程序继续: Got to the end 如果我尝试使用shell和一个不以0退出的命令,则try不会捕获它: try shell('/usr/bin/false');say "Got to the end"; 输出看起来不像是一个例外: The spawned c
这 try捕获异常:

try die X::AdHoc;
say "Got to the end";

输出显示程序继续:

Got to the end

如果我尝试使用shell和一个不以0退出的命令,则try不会捕获它:

try shell('/usr/bin/false');
say "Got to the end";

输出看起来不像是一个例外:

The spawned command '/usr/bin/false' exited unsuccessfully (exit code: 1)
  in block <unit> at ... line ...

这是怎么回事呢?

解决方法

答案真的由Jonathan Worthington提供:

https://irclog.perlgeek.de/perl6-dev/2017-04-04#i_14372945

简而言之,shell()返回一个Proc对象.对象沉没的那一刻,如果运行程序失败,它将抛出内部异常.

$6 'dd shell("/usr/bin/false")'
Proc.new(in => IO::Pipe,out => IO::Pipe,err => IO::Pipe,exitcode => 1,signal => 0,command => ["/usr/bin/false"])

所以,你需要做的是在变量中捕获Proc对象,以防止它被沉没:

$6 'my $result = shell("/usr/bin/false"); say "Got to the end"'
Got to the end

然后你可以使用$result.exitcode来查看它是否成功.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读