Take the 2-minute tour ×

If I have a function with a try/finally section, and the thread running it is interrupted while in the try block, will the finally block execute before the interruption actually occurs?

share|improve this question
You should clear up what you mean by 'interrupted' - this word is overloaded :( It means different things to firmware/driver developers, OS developers and java developers. –  Martin James Jan 29 '13 at 9:10
Well, I tagged the question as "java", and I mentioned try/finally, so I figured one can understand that I'm talking about interrupting threads in Java... –  user940016 Jan 29 '13 at 12:25

4 Answers 4

up vote 1 down vote accepted

According to the Java Tutorials, "if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues."

Here's the full passage:



class Thread1 implements Runnable {

    public void run() {
        try {
        } catch (InterruptedException e) {
        } finally {
            System.out.println("finally executed");



It prints - finally executed

share|improve this answer
Hmm... I can understand finally not getting executed if the thread is killed. However, the thread being interrupted should NOT stop the finally clause from being executed. I'm not sure why this passage says that. Here's the JLS description in its full glory: docs.oracle.com/javase/specs/jls/se7/html/… –  sjlee Jan 29 '13 at 16:34
@sjlee - Thanks for comment, I have tested in code. Its working as I said. –  Subhrajyoti Majumder Jan 29 '13 at 17:27
OK, you mean that finally DOES get executed even if the thread is interrupted, correct? If so, your answer needs to be clarified. It sounds as though finally may not execute if the thread is interrupted. –  sjlee Jan 29 '13 at 19:07
One had assumed it is referring to the native thread to which the java thread is bound being interrupted at the system level, not a java thread interrupt. –  Affe Jan 29 '13 at 22:27
Actually, it only works because the Interruption is catched. You are not properly handling the InterruptedException as you are simply swallowing it. You should propagate it either by re-throwing or by calling Thread.currentThread.interrupt(). For more information, see ibm.com/developerworks/java/library/j-jtp05236/index.html . The question then is - will the finally still be executed? –  Risadinha Oct 10 '13 at 16:57

The effect of interruption is to throw an InterruptedException the next time a blocking operation occurs (in practice, the next time a method is called that specifies it can throw an InterruptedException), at which point -- as usual -- the normal try/catch execution flow is followed, which does indeed execute the finally block after the try and any applicable catches.

share|improve this answer

A Thread Interrupt in Java is just setting a flag. It doesn't cause anything special to happen to currently executing code, or affect the flow of control.

If your thread is engaged in, or attempts to enter, an operation that throws InterruptedException, then the exception is thrown from the point where that method is invoked and if it's inside a try block, the finally will execute before the exception leaves just like normal.

share|improve this answer

It will execute the same way as with any other exception from the try block, not before the interruption.

share|improve this answer

Your Answer


