如果 catch 和 finally 块都抛出异常会发生什么?

发布于 2024-08-06 02:46:31 字数 38 浏览 8 评论 0原文

如果 catch 和 finally 块都抛出异常会发生什么?

What happens if both catch and finally blocks throw exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

策马西风 2024-08-13 02:46:31

finally块抛出异常时,它将有效地隐藏从catch块抛出的异常,并且将是最终抛出的异常。因此,在捕获异常时记录异常,或者确保finally 块本身不会抛出异常,这一点很重要,否则您可能会抛出被抑制且从未见过的异常​​。

When the finally block throws an exception, it will effectively hide the exception thrown from the catch block and will be the one ultimately thrown. It is therefore important to either log exceptions when caught, or make sure that the finally block does not itself throw an exception, otherwise you can get exceptions being thrown that are stifled and never seen.

一桥轻雨一伞开 2024-08-13 02:46:31

当catch抛出异常时,finally块将被运行,然后异常退出。
如果finally 块抛出异常,则该块将因异常而退出。

When catch throws an exception, finally block will be run and then exit with an exception.
If the finally block throws an exception, the block will exit with an exception.

雨落□心尘 2024-08-13 02:46:31

最后抛出的异常被抛出。

The last exception thrown is thrown.

场罚期间 2024-08-13 02:46:31

adrianbanks 已经很好地回答了这个问题,但是下面的帖子应该很有趣:
有趣的异常结果:从 Final 块抛出异常

Its already been answered well by adrianbanks, but the following post should be interesting:
Interesting Exception Results: Throwing Exceptions From the Finally Block

话少心凉 2024-08-13 02:46:31

嗨,Nwaman,我认为你的答案是错误的,我已经在 Windows 应用程序中测试了它,我发现如果你编写如下所示的程序

try
{
    string s = "hu";
    int i = int.Parse(s);
}
catch (Exception ex)
{
    string s = "hu";
    int i = int.Parse(s);
    throw new Exception();
}
finally
{
    MessageBox.Show("hi");
}

,这将不会最终执行,

HI Nwaman i think you answer is wrong i have tested it in windows appliaction, i found if u write a program like the below one

try
{
    string s = "hu";
    int i = int.Parse(s);
}
catch (Exception ex)
{
    string s = "hu";
    int i = int.Parse(s);
    throw new Exception();
}
finally
{
    MessageBox.Show("hi");
}

and this will not result finally to excute,

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