最终阻止真正需要清理代码(如关闭流)?

编程入门 行业动态 更新时间:2024-10-25 17:20:41
本文介绍了最终阻止真正需要清理代码(如关闭流)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很困惑,为什么我需要把清理代码放在一个终止块中。

I am very confused as to why do I need to need to put the clean-up code like closing streams in a finally block.

我已经看到,无论什么(无论是否有例外),最终块中的代码将会运行;并且在 finally 块运行之后,该方法的其余部分将继续。

I've read that the code in finally block will run no matter what (whether there's an exception); and after the finally block runs, the rest of the method continues.

我的问题是:如果剩下的方法必须继续,那么为什么在函数中的try / catch块之后我不会把清理代码?

My question is: if the rest of the method has to continue then why don't I put the clean-up code after my try/catch block in a function?

推荐答案

如果抛出未捕获的异常,finally块将始终运行,但该方法中的其余代码将被跳过

The finally block will always run if an uncaught exception is thrown, but the rest of the code in the method will be skipped.

所以如果在finally块之后放置清理代码,如果有异常,它将不会被调用。

So if you put clean-up code after the finally block, it won't get called if there is an exception.

更多推荐

最终阻止真正需要清理代码(如关闭流)?

本文发布于:2023-11-15 23:12:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!