hi! acco to documentation, if any exception is thrown in the finalize() method then finalization terminates, it doesn't state what happens to the object?? I think eventually the object is garbage collected.(but remember finalize() will not be called again). that's it. ashok.
Originally posted by Angela Narain: If in the finalize() method , some exception is thrown, then will the object still be garbage collected ?
GC will call the finalize() as the last method and only once thats all. Wheather the object will be removed ie gced is not affected by finalize()or its thrown exceptions anyway. However finalize() can resurrect the object back in action if it wants to
Hi Angela, If an exception is thrown during finalize(), the finalize() method is halted, the exception is ignored and gc will continue on. The finalize() method is there strictly as a convenience. The default Object.finalize() method doesn't do anything. The developers just provided it 'in case' you want to do something ie release non-gc'd resources, before an object is gc'd. As far as the language is concerned, finalization is not critical to gc. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Thanks Jane, I was not clear on why "..finalize() is halted " We can catch exceptions in the finalize block.
I suppose finalize() is more for closing system resources before the object gets GCed. While System.gc() on the other hand is for freeing up the memory [This message has been edited by Angela Narain (edited October 03, 2001).]