method finalize and exceptions

后端 未结 3 693
梦谈多话
梦谈多话 2021-01-22 09:56

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 metho

相关标签:
3条回答
  • 2021-01-22 10:24

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

    0 讨论(0)
  • 2021-01-22 10:27

    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.

    0 讨论(0)
  • 2021-01-22 10:32

    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.

    0 讨论(0)
提交回复
热议问题