Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on finalize() method

 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Plz help me with a right answer for the question pasted below.
If a Runtime Exception is thrown in the finalize method -
1. The running application crashes.
2.The exception is simply ignored and the object is garbage collected.
3.The exception is simply ignored, but the object is not garbage collected.
4.The Exception causes the JVM to crash.
Ans is 2.
My justification -
According to me the option '3' is right. I agree to statement that 'exception is simply ignored'. When the finalize method is invoked for the first time the exception is thrown, the exception is ignored & the object garbage collection exit's. Next time the garbage collector runs it will not invoke the finalize() method and the object could be garbage collected.
Thanks,
Reshma
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me the option '3' is right. I agree to statement that 'exception is simply ignored'. When the finalize method is invoked for the first time the exception is thrown, the exception is ignored & the object garbage collection exit's. Next time the garbage collector runs it will not invoke the finalize() method and the object could be garbage collected.

The exception is simply ignored. thats right...however, the object is garbage collected.unless u resurrect the object again, there no reason for the garbage collector not to collect it. It'll certainly do it.
regards,
Rashmi
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rashmi
This is what the javadoc mentions

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

reshma
[ March 21, 2003: Message edited by: Reshma Pai ]
 
sharana sharana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi reshma ,
this is is y i think answer 2 is right
As stated in JLS,
If a class does not override method finalize of class Object (or overrides it in only a trivial way)then if instances of such a class become unreachable, they may be discarded immediately rather than made to await a second determination that they have become unreachable.
the finalizable object was reachble only by the thread which invoked finalize() method of the Object.Becuz of exception the Object is abruptly completed and hence BECOMES UNREACHABLE , as JVM says finalize() method of an Object will never be invoked more than once in its life time.

If the Java virtual machine detects that a finalized object has become unreachable, it may reclaim the storage occupied by the object because the object will never again become reachable .
 
sharana sharana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops typo!!!in above posting - so reposting it
hi reshma ,
this is is y i think answer 2 is right
As stated in JLS,
If a class does not override method finalize of class Object (or overrides it in only a trivial way)then if instances of such a class become unreachable, they may be discarded immediately rather than made to await a second determination that they have become unreachable.
the finalizable object was reachble only by the thread which invoked finalize() method of the Object.Becuz of exception the finalize() method is abruptly completed and hence BECOMES UNREACHABLE [ since oonly thread which has accessing to it gaveup ]. JVM says finalize() method of an Object will never be invoked more than once in its life time.

If the Java virtual machine detects that a finalized object has become unreachable, it may reclaim the storage occupied by the object because the object will never again become reachable .
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of the answers are correct. As Reshma quoted above, the exception is ignored but the finalization is halted. The garbage collection process can continue even if finalization is halted. However, there is no guarantee that the garbage collector will complete the process. For example, if a higher priority thread is ready to run then the garbage collection process might not be completed.
It is correct to say that the exception would cause the finalization process to be halted, but it is not possible to guarantee that the garbage collection process will or will not run.
The real exam will test your knowledge of what is guaranteed. It will not test your knowledge of might or might not happen.
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Dan for helping me out.

Reshma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic