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

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

概述这 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 '/us 这 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对象.对象沉没的那一刻,如果运行程序失败,它将抛出内部异常.

 '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对象,以防止它被沉没:

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

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

总结

以上是内存溢出为你收集整理的异常 – 为什么Perl 6尝试不在shell()中处理非零退出?全部内容,希望文章能够帮你解决异常 – 为什么Perl 6尝试不在shell()中处理非零退出?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1271308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存