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).
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.
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.
Originally posted by Anvi Dixit: then what does finalization of object gets halt line means
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.
Meena R. Krishnan
Joined: Aug 13, 2006
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.
Joined: Aug 28, 2006
I got the point ..and now i am very clea with this topic
with regards, Anvi
subject: finalize() method invoked by the garbage collector