Are there any details on whether or not an object is cleaned up using finalize() if that object’s constructor thew an exception.
When this method is called is notoriously ill defined. According to the manual:
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is guaranteed,
however, that the thread that invokes finalize will not be holding any
user-visible synchronization locks when finalize is invoked. If an
uncaught exception is thrown by the finalize method, the exception is
ignored and finalization of that object terminates.
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize%28%29
I’ve not been able to trigger the finalize method in this way. Does anyone know if it is garunteed NOT to be called or if it is in some cases called after the constructor failed to initialize the object (thew an exception).
I ask this because I have an object which must not be cleaned up twice. I’m trying to understand if it is safe to clean up before throwing the exception or if I must leave a marker for finalize() to effectively skip and do nothing.
My test shows that it can
prints
it is on Java HostSpot Client VM 1.7.0_03