如果 catch 和 finally 块都抛出异常会发生什么?
如果 catch 和 finally 块都抛出异常会发生什么?
What happens if both catch and finally blocks throw exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果 catch 和 finally 块都抛出异常会发生什么?
What happens if both catch and finally blocks throw exception?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
当
finally
块抛出异常时,它将有效地隐藏从catch
块抛出的异常,并且将是最终抛出的异常。因此,在捕获异常时记录异常,或者确保finally 块本身不会抛出异常,这一点很重要,否则您可能会抛出被抑制且从未见过的异常。When the
finally
block throws an exception, it will effectively hide the exception thrown from thecatch
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.当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.
最后抛出的异常被抛出。
The last exception thrown is thrown.
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
嗨,Nwaman,我认为你的答案是错误的,我已经在 Windows 应用程序中测试了它,我发现如果你编写如下所示的程序
,这将不会最终执行,
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
and this will not result finally to excute,