CATCH失败失败

Hol*_*lli 7 error-handling perl6 raku

我可能忽略了一些简单的事情,但是我不希望下面的代码失败。它表现得好像是我写的,die而不是fail在catch块中的。

无法正确处理故障,代码消失。

sub foo() 
{
  try {
    say 1 / 0;
    CATCH { default { fail "FAIL" } }
  } 

  return True;
}

with foo() { 
    say "done";
}
else
{
  say "handled {.exception.message}"
}
Run Code Online (Sandbox Code Playgroud)

输出:

FAIL
  in block  at d:\tmp\x.pl line 5
  in any  at d:\tmp\x.pl line 5
  in sub foo at d:\tmp\x.pl line 4
  in block <unit> at d:\tmp\x.pl line 11
Run Code Online (Sandbox Code Playgroud)

rai*_*iph 6

为了带给后来的读者尤达在评论中所说的全部力量,最简单的解决方案是弄不懂try为了做到的观念CATCH。您不会:

sub foo() 
{
  say 1 / 0;
  CATCH { default { fail "FAIL" } }
  return True;
}

with foo() { 
    say "done";
}
else
{
  say "handled {.exception.message}"
}
Run Code Online (Sandbox Code Playgroud)

正确显示:

handled FAIL
Run Code Online (Sandbox Code Playgroud)