我有这个:
try { run 'tar', '-zxvf', $path.Str, "$dir/META6.json", :err }
Run Code Online (Sandbox Code Playgroud)
尽管处于try{}阻塞状态,这一行仍然导致我的脚本崩溃:
The spawned command 'tar' exited unsuccessfully (exit code: 1, signal: 0)
in block at ./all-versions.raku line 27
in block at ./all-versions.raku line 16
in block <unit> at ./all-versions.raku line 13
Run Code Online (Sandbox Code Playgroud)
为什么该try{}块不允许脚本继续执行以及如何让它继续执行?
那是因为run(还)没有失败。 run返回一个Proc对象。而且它本身还不会抛出异常。
try只是返回该Proc对象。然而,一旦使用返回值(例如,通过使其下沉),它就会抛出。
比较(与立即下沉):
$ raku -e 'run "foo"'
The spawned command 'foo' exited unsuccessfully (exit code: 1, signal: 0)
Run Code Online (Sandbox Code Playgroud)
和:
$ raku -e 'my $a = run "foo"; say "ran, going to sink"; $a.sink'
ran, going to sink
The spawned command 'foo' exited unsuccessfully (exit code: 1, signal: 0)
Run Code Online (Sandbox Code Playgroud)
现在,导致Proc在代码中使用该对象的原因尚不清楚。您必须显示更多代码。
检查是否成功的一种方法是检查exit-code:
$ raku -e 'my $a = run "foo"; say "failed" if $a.exitcode > 0'
failed
$ raku -e 'my $a = run "echo"; say "failed" if $a.exitcode > 0'
Run Code Online (Sandbox Code Playgroud)
或者,使用 Jonathan 的解决方案:
$ raku -e 'try sink run "foo"'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |