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?
Joined: Jul 03, 2001
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.