aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() method invoked by the garbage collector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize() method invoked by the garbage collector" Watch "finalize() method invoked by the garbage collector" New topic
Author

finalize() method invoked by the garbage collector

Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Hello,
please go through this example. after running this code what i got is the program gets terminate.

now here the concept is : If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.

can anyone tell me what does the uncaught exception is ?
if the finalize method has thrown the null pointer exception will the problem gets solved i mean the exception is not ignored . am i correct?

thanks
Anvi
[ September 16, 2006: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you look at your finalize method you will see that you create an Exception object and throw it. The exception is not caught because there is no try-catch statement in your finalize method. So here we have an "uncaught" exception ("un-catched" ). If there was more code after throwing the exception in your finalize method, then it would not be executed - control goes back to the garbage collector code. The garbage collector will handle the exception by just ignoring it (discarding the exception) and attempt to remove the object from the heap. I cannot see any reason why a NullPointerException would be treated differently because finalize() declares that it throws Throwables (Exceptions and Errors).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Secondly, it may not even get that far....

Remember that GC is not guaranteed. When the main method finished, which is immediately right after creating the 5 test objects, the main thread will terminate shortly. And since there are not other user threads, the JVM should exit.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Thank you Henry and Barry

but there is still some more confusions. if you are able to clear me then i'll be very thnx to you

if some code is written in the finalize method and no exception is thrown then what will be the scenario. also there is a line i found in khalid mughal book
"Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored."
here either the program gets halt or the exception is ignored. but the code which i have posted in my first mail when runs both things are happening exception is ignored and the program is terminated..
why this happens.

thanking you

anvi
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Program termination is different from 'finalization of object being halted'.

In your case the program itself terminates normally and the main thread exits immediately after the creation of the test objects and GC is not guaranteed to run within that time.
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
then what does finalization of object gets halt line means

Regards
Anvi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Originally posted by Anvi Dixit:
then what does finalization of object gets halt line means

Regards
Anvi


Hard to be sure, since we don't know if you are quoting the author out of context...

My guess is... he is saying that when a method returns or throws an exception, it is done. Whatever it is doing (in this case finalization) is done. Maybe he used the word "halted" because it is an exception condition -- not sure.

Henry
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
then what does finalization of object gets halt line means


The finalize() method is normally overridden in order to do a cleanup and to free any system resources. During that process in finalize() method, in the middle for any reason an exception is thrown then the rest of the method will not be executed thereby an incomplete finalization.
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Thanks Krishnan

I got the point ..and now i am very clea with this topic

with regards,
Anvi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize() method invoked by the garbage collector