method finalize and exceptions

若如初见. 提交于 2019-12-20 03:15:09

问题


I don't understand very well when an exception is ignored by the GC when it reclaims from the memory an object.

If I have a try/catch into a finalize method I see it is always executed... so which are the cases where the exception is not thrown?

Thanks.


回答1:


the finalize method is run by the finalizer thread. if you throw exception, the finalizer will ignore it (swallow it). Otherwise, the finalizer thread would die. This applies to exceptions that are thrown and not caught by your code (inside finalize()). If you catch the exception, it is business as usual.




回答2:


It means that any exception thrown from the finalize method is ignored. However, exceptions inside it still work as usual.




回答3:


The two existing answers appear to say that the finalizer will ignore any uncaught exceptions. This appears to contradict the answer here: Exception in finalize method which appears to have a correct reference to the JSL. It says that uncaught exceptions will abort the finalization of the object concerned (possibly leaking resources) but that the finalizer thread itself will continue finalizing other objects. This matches empirical results.



来源:https://stackoverflow.com/questions/6276289/method-finalize-and-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!