我很困惑,为什么我需要把清理代码放在一个终止块中。
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.
更多推荐
最终阻止真正需要清理代码(如关闭流)?
发布评论