Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Reg. finalize  RSS feed

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If in the finalize() method , some exception is thrown, then will the object
still be garbage collected ?
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! acco to documentation, if any exception is thrown in the finalize() method then finalization terminates, it doesn't state what happens to the object?? I think eventually the object is garbage collected.(but remember finalize() will not be called again). that's it.
ashok.

Originally posted by Angela Narain:
If in the finalize() method , some exception is thrown, then will the object
still be garbage collected ?


 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and the Java Language Specification says that Exception is unreported.
you may check some detail here also... http://www.artima.com/java/answers/Dec2000/messages/8.html

Originally posted by Angela Narain:
If in the finalize() method , some exception is thrown, then will the object still be garbage collected ?

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GC will call the finalize() as the last method and only once thats all. Wheather the object will be removed ie gced is not affected by finalize()or its thrown exceptions anyway. However finalize() can resurrect the object back in action if it wants to
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
If an exception is thrown during finalize(), the finalize() method is halted, the exception is ignored and gc will continue on.
The finalize() method is there strictly as a convenience. The default Object.finalize() method doesn't do anything. The developers just provided it 'in case' you want to do something ie release non-gc'd resources, before an object is gc'd.
As far as the language is concerned, finalization is not critical to gc.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane,
I was not clear on why "..finalize() is halted "
We can catch exceptions in the finalize
block.

I suppose finalize() is more for closing system
resources before the object gets GCed.
While System.gc() on the other hand is for freeing up the memory
[This message has been edited by Angela Narain (edited October 03, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!