方法finalize和例外

方法finalize和例外

本文介绍了方法finalize和例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我有一个 try / catch 到一个finalize方法我看到它总是执行...所以这些异常是不会抛出的情况?



谢谢。

解决方案

finalize 线。如果你抛出异常,终结器会忽略它(吞下它)。否则,终结器线程将死亡。
这适用于抛出并且未被代码捕获的异常( finalize()内)。如果你抓住这个例外情况,就像往常一样。


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.

解决方案

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.

这篇关于方法finalize和例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:37