File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize() method of garbage collection...HELP needed....

 
vikas mehta
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"If an exception is thrown during execution of the finilize() method of an object then the exception is ignored and the object destroyed."
this is a false statement according to khalid mughal Q 8.3

according to java language specification 2.0....
"If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates."
i m not able to understand what actually happens...
as again the khalid mughal in the answer to Q 8.3 says...exceptions are not really ignored but handled normally...
plz help.
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The exceptions raised in finalize() are ignored. Meaning the JVM will not try search the stack to find the exception handler."

happy new year.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3664
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"...the exception is ignored and finalization of that object terminates."
This does not clearly tell what happens to the object if the finalization terminates due to an exception. I think, it means "finalization terminates as if nothing (no exception) happened", in which case the object will be destroyed.
HTH,
Paul.

------------------
SCJP2, SCWCD Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
JQPlus - For SCJP2
JWebPlus - For SCWCD
JDevPlus - For SCJD
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the exception is thrown in a finalizer but not handled then it is ignored by JVM
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the exception is thrown in a finalizer but not handled then it is ignored by JVM ........
and even if it is handled ok then also it is ignored. right ? is this correct as well ?
Originally posted by Ragu Sivaraman:
If the exception is thrown in a finalizer but not handled then it is ignored by JVM

 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The gc calls finalize() on a fully dereferenced object, and catches any exception that finalize() throws. Since Object.finalize() throws Throwable, you can guess that the gc is ready for anything.
Ignoring uncaught exceptions is the VM's way of saying finalize() can't get around it by throwing "the unexpected." That is, you cannot write a garbage collector that would throw an uncatchable finalize() exception and cause the VM to crash. But if the exception is caught, there's no reason not to process it.
Another way to read this rule: the gc uses a finally block (or its functional equivalent) to guarantee it has the last word on collection.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Michael ,
i have some doubt about it too.
it is very clear now.


[This message has been edited by FEI NG (edited January 01, 2002).]
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a nutshell: if an exception is thrown when gc executes finalize, this method ends. But the object is recycled as if no exception was thrown (or finalize would have completed normally)
[This message has been edited by Jose Botella (edited January 01, 2002).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic