• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

finalize() method invoked by the garbage collector

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what does finalization of object gets halt line means

Regards
Anvi
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Krishnan

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

with regards,
Anvi
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic