Course contentsShow
← Java
Lesson 2597 of 2,87048. Concurrency FundamentalsFree lesson

Common Mistakes: Ignoring InterruptedException

Why catching and ignoring InterruptedException is problematic and loses interrupt signal.

Common Mistakes: Ignoring InterruptedException

You'll learn why catching InterruptedException but doing nothing with it destroys your thread's ability to respond to cancellation requests.

The Core Problem

When a thread is interrupted during sleep() (or similar blocking operations), Java throws InterruptedException to let you know someone wants this thread to stop what it's doing. This is a cooperative cancellation mechanism—the interrupting thread is politely asking you to clean up and exit.

The mistake? Catching the exception but then continuing as if nothing happened:

try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    // WRONG: Silently ignoring it
}
// Code continues executing...

Why This Is Problematic

When you ignore InterruptedException, you're swallowing the interrupt signal. The thread that tried to interrupt you thinks the message was delivered, but your thread just keeps running. It's like someone pulling a fire alarm and you saying "interesting noise" and going back to work.

The interrupt was meant to be a cooperative shutdown request. By ignoring it, you've made your thread unresponsive to cancellation. In frameworks or thread pools, this can prevent graceful shutdown and leave threads running when they should have stopped.

What You Should Do Instead

At minimum, restore the interrupt status so calling code knows about it:

try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    Thread.currentThread().interrupt(); // Restore interrupt status
    // Then exit or return appropriately
}

Key Takeaway: Never catch and ignore InterruptedException—it's a cancellation request. Either propagate it, restore the interrupt status with Thread.currentThread().interrupt(), or handle it by terminating your task gracefully.