Responding to InterruptedException: Best Practices
What You'll Learn
You'll learn when to propagate InterruptedException, restore the interrupt flag, or handle interruption locally inside catch blocks.
Three Ways to Respond
When you catch an InterruptedException, you have three main options, each appropriate for different scenarios:
1. Propagate the Exception
If your method can declare throws InterruptedException, let it bubble up. This is the cleanest approach when your method is part of a larger interruptible operation:
public void performTask() throws InterruptedException {
try {
Thread.sleep(1000);
// other work
} catch (InterruptedException e) {
// Optionally clean up resources
throw e; // Let caller handle it
}
}
2. Restore the Interrupt Flag
When you cannot throw a checked exception (implementing Runnable, using streams, etc.), restore the interrupted status so higher-level code can detect it:
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore the flag
// Handle gracefully (e.g., exit loop, return)
}
}
Think of this as passing a message up the chain — you're saying "I was interrupted" even though you can't throw the exception.
3. Handle Locally (Rare)
Only swallow the exception if your thread truly owns the interruption policy and knows exactly what to do:
catch (InterruptedException e) {
// Only if you're certain this is the final handler
cleanupAndExit();
}
The Anti-Pattern
Never ignore interruption by catching and doing nothing — this breaks the interruption contract and can leave threads stuck indefinitely.
Key Takeaway: Always respond to InterruptedException by either propagating it, restoring the interrupt flag with Thread.currentThread().interrupt(), or handling it deliberately as the final authority—never silently ignore it.