一个 CATCH 块捕获多少个异常?
如果一个 try{} 块中发生多个异常,并且该 try{} 块内只有一个 CATCH{} 块,那么该 CATCH{} 块是否可以捕获任何/所有异常?或者我是否需要针对每个可能的异常使用一个 CATCH{}?
try { CATCH { default { say "seenError" }; }; die "1"; die "2"; die "3" }
seenError
在上面的例子中,哪个“die”语句被捕获了?第一?如果我想处理每个异常,是否需要将每个可能的异常包含在一个单独的 CATCH{} 中?
If multiple exceptions occur in a try{} block and there is only one CATCH{} block inside the try{} block, can this CATCH{} block catch any/all the exceptions? Or do I need one CATCH{} for each possible exception?
try { CATCH { default { say "seenError" }; }; die "1"; die "2"; die "3" }
seenError
In the example above, which "die" statement was caught? 1st? If I want to handle each exception, do I need to enclose each possible exception within one separate CATCH{} ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先:您不需要需要
try
来拥有CATCH
块。在 Raku 编程语言中,CATCH
块可以存在于任何词法范围内。所以你的例子可以变成:
如果你像这样运行它,它只会显示“1”,这意味着只捕获了第一个异常。这是正确的,因为您不更改有关情况的任何内容:异常仍然处于活动状态,并将导致程序终止。
您需要的是在处理异常后恢复执行。有一个方法可以做到这一点:
.resume
。它并不适用于所有异常,但随后您会被告知它不起作用。将程序更改为:
这将显示“1”、“2”、“3”、“仍然活着”。
那么
try
是做什么的?基本上,它会为您添加一个CATCH
块,该块将捕获它看到的第一个异常,将异常放入$!
中并返回Nil
。您可以将try
块视为将致命异常转变为良性故障的一种方法。如果您对失败的原因不感兴趣,则可以使用它。并且try
也可以是语句前缀。简而言之:First of all: you do NOT need a
try
to have aCATCH
block. In the Raku Programming Language, aCATCH
block can live in any lexical scope.So your example can become:
If you run it like this, it will just say "1", implying that only the first exception was caught. And that is correct, because you do NOT change anything about the situation: the exception is still active and will cause the termination of your program.
What you need, is to resume execution after the exception was handled. And there's a method for that:
.resume
. It doesn't work on all exceptions, but then you will be told it doesn't work.Changing the program to:
This will show "1", "2", "3", "still alive".
So what does
try
do? Basically, it adds aCATCH
block for you that will catch the first exception that it sees, put the exception in$!
and return withNil
. You could think of atry
block as a way to turn a fatal exception into a benign failure. You use it if you're not interested in why something failed. Andtry
can also be a statement prefix. In short:(请不要忘记接受 Liz 的答案。这只是她答案的附录。)
正如 Liz 所说:
而且,正如她暗示的那样,你也不需要一个
CATCH
> 使用try
时阻止:显示:
正如 Liz also 所概述的,如果在处理正在处理的代码期间抛出异常且未
.resume
d尝试
d,或者如果有未处理的Failure
,try
捕获异常/失败,将相应的异常放入$!
中,并返回Nil
。我提到这一点是为了介绍一个可能感兴趣的最后一个选项:
trys
,这是我为我的回答创建的SOQ 从异常处理程序返回值。(Please don't forget to accept Liz's answer. This is just an addendum to her answer.)
As Liz notes:
And, as she implies, neither do you need a
CATCH
block when usingtry
:displays:
As Liz also outlines, if an exception is thrown and not
.resume
d during processing of code that's beingtry
d, or if there's an unhandledFailure
, thetry
catches the exception/failure, puts the corresponding exception in$!
, and returnsNil
.I mention this to introduce one final option that might be of interest:
trys
, which I created for my answer to the SOQ Returning values from exception handlers.