一个 CATCH 块捕获多少个异常?

发布于 2025-01-11 20:04:15 字数 294 浏览 0 评论 0原文

如果一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2025-01-18 20:04:15

首先:您不需要需要try来拥有CATCH块。在 Raku 编程语言中,CATCH 块可以存在于任何词法范围内。

所以你的例子可以变成:

CATCH {
    default {          # $_ contains the exception
        say .message;  # show what the message was
    }
}
die "1";
die "2";
die "3";
say "still alive";

如果你像这样运行它,它只会显示“1”,这意味着只捕获了第一个异常。这是正确的,因为您更改有关情况的任何内容:异常仍然处于活动状态,并将导致程序终止。

您需要的是在处理异常后恢复执行。有一个方法可以做到这一点:.resume。它并不适用于所有异常,但随后您会被告知它不起作用。

将程序更改为:

CATCH {
    default {
        say .message;
        .resume;  # continue execution after where exception occurred
    }
}
die "1";
die "2";
die "3";
say "still alive";

这将显示“1”、“2”、“3”、“仍然活着”。

那么 try 是做什么的?基本上,它会为您添加一个 CATCH 块,该块将捕获它看到的第一个异常,将异常放入 $! 中并返回 Nil 。您可以将 try 块视为将致命异常转变为良性故障的一种方法。如果您对失败的原因不感兴趣,则可以使用它。并且 try 也可以是语句前缀。简而言之:

say try die;  # Nil

First of all: you do NOT need a try to have a CATCH block. In the Raku Programming Language, a CATCH block can live in any lexical scope.

So your example can become:

CATCH {
    default {          # $_ contains the exception
        say .message;  # show what the message was
    }
}
die "1";
die "2";
die "3";
say "still alive";

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:

CATCH {
    default {
        say .message;
        .resume;  # continue execution after where exception occurred
    }
}
die "1";
die "2";
die "3";
say "still alive";

This will show "1", "2", "3", "still alive".

So what does try do? Basically, it adds a CATCH block for you that will catch the first exception that it sees, put the exception in $! and return with Nil. You could think of a try 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. And try can also be a statement prefix. In short:

say try die;  # Nil
亢潮 2025-01-18 20:04:15

(请不要忘记接受 Liz 的答案。这只是她答案的附录。)

正如 Liz 所说:

不需要需要try来拥有CATCH

而且,正如她暗示的那样,你也不需要一个CATCH > 使用 try 时阻止:

for 1, 2, 3 {
  try { print 'something ... '; .&die } or put "seenError: $!"
}

显示:

something ... seenError: 1
something ... seenError: 2
something ... seenError: 3

正如 Liz also 所概述的,如果在处理正在处理的代码期间抛出异常且未 .resumed 尝试d,或者如果有未处理的 Failuretry 捕获异常/失败,将相应的异常放入$!中,并返回Nil

我提到这一点是为了介绍一个可能感兴趣的最后一个选项:trys,这是我为我的回答创建的SOQ 从异常处理程序返回值

(Please don't forget to accept Liz's answer. This is just an addendum to her answer.)

As Liz notes:

you do NOT need a try to have a CATCH block

And, as she implies, neither do you need a CATCH block when using try:

for 1, 2, 3 {
  try { print 'something ... '; .&die } or put "seenError: $!"
}

displays:

something ... seenError: 1
something ... seenError: 2
something ... seenError: 3

As Liz also outlines, if an exception is thrown and not .resumed during processing of code that's being tryd, or if there's an unhandled Failure, the try catches the exception/failure, puts the corresponding exception in $!, and returns Nil.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文