This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finally" Watch "finally" New topic


Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
The circumstances that can prevent execution of the code in a finally block are
A) The death of the thread
B) The use of System.exit()
C) It is always guaranteed to be executed.
I thought that the answer is only B. But the Mock has given both A and B are correct.
Can some one explain when the death of the thread can come in to picture?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
It seems sensible to think that the death of the thread running the the try clause will prevent finally to execute. However in practice it won't.
a) When the deprecated Thread.stop method is called The error ThreadDeath is thrown over that thread. Still finally is executed. In fact finally is executed even if a Throwable is thrown. This code prints Hello

b) If the thread were interrupted within a try the only serious consequence will be --if the thread were waiting, joinning or sleeping-- the throwing of an InterruptedException. But, again, no Throwable will prevent the execution of finally.
c) If the thread happens to end its task within the try clause, it can falls through the end of try, or return. None will prevent finally execution.
Summing up: I do not know how to stop or cause the death of a thread running a try, and at the same time only prevent finally execution. After all, System.exit will stop the whole application.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: finally
jQuery in Action, 3rd edition