如果在finalize()期间抛出异常会发生什么
如果finalize()执行过程中抛出异常会发生什么? 堆栈展开是否像平常一样?它会继续finalize()并忽略异常吗?它会停止 Finalize() 并继续 GC 对象吗?或者其他什么?
我不是在寻找使用 Finalize() 的指南,有很多页面对此进行了解释。
What would happen if an exception is thrown during the execution of finalize()?
Is the stack unwind like normally? Does it continue finalize() and ignore the exception? Does it stop finalize() and continue GC the object? Or something else?
I'm not looking for guidelines of using finalize() there are plently of pages explaining that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 Object#finalize() javadoc:
From the Object#finalize() javadoc:
假设您有充分的理由编写终结器,则编写终结器的正确方法是:
The correct way to code a finalizer, assuming you have a valid reason to write one at all, is this:
如果抛出异常,那么finalize的调用将被终止,下次它不会被调用,但对象将从内存中被GC。
In case if exception would be thrown, then the invocation of finalize will be terminated, and next time it will not be invoked but object will be GC-ed from memory.