aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finalization Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finalization Method" Watch "Finalization Method" New topic
Author

Finalization Method

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Regarding Finalization Method:
If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.
My Question is what happens next to the object?
After the finalization terminates, does the objects get destroyed or it does nothing??
Sonir
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
The object gets reclaimed by the GC, only the finalization process is aborted at the point the "uncaught" exception occurs in the finally clause.
--
Rajinder Yadav


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Sonir :
Was your question in reference to the finally block of code or was it in reference to the finalize method, which is typically called by the garbage collector after the object is no longer accessible from any reference?
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
finalize() method..
??
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
I thought so too. Your question has nothing to do with the finally block. Finalization can happen on objects that do not have any try/catch/finally blocks at all.
This is what the API states of finalize method

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.
After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

I believe this means,that even if an exception occurs in the finalize method, the object still remains eligible for garbage collection and it will be garbage collected eventually.
HTH
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
I guess I read the question wrong, so to clear up my original reply
If the uncaught exception occurs in the clean up of the object's finialize() method then the current execution of the program is aborted, but the exception is ignored!
btw, there is nothing preventing a try/catch/finally block from appearing within a finalize method.
[ January 21, 2002: Message edited by: Rajinder Yadav ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finalization Method
 
Similar Threads
Finalize doubt
Exception in finalize()
Garbage Collection
another question regarding garbage collection
finalize method