Why must I wrap every Thread.sleep() call in a try/catch statement?
<p>I am trying to write my first multi-threaded program in Java. I can't understand why we require this exception handling around the for loops. When I compile without the try/catch clauses it gives an <code>InterruptedException</code>.</p>
<p>Here is the message:</p>
<pre><code>Exception in thread &quot;main&quot; java.lang.Error: Unresolved compilation problem: 
Unhandled exception type InterruptedException
</code></pre>
<p>But when run with the try/catch, the sysout in the catch blocks are never displayed - implying no such exception was caught anyway!</p>
<pre><code>public class SecondThread implements Runnable {
    Thread t;
    
    SecondThread() {
        t = new Thread(this, &quot;Thread 2&quot;);
        t.start();
    }

    public void run() {
        try {
            for (int i=5; i&gt;0; i--) {
                System.out.println(&quot;thread 2: &quot; + i);
                Thread.sleep(1000);
            }
        }
        catch (InterruptedException e) {
            System.out.println(&quot;thread 2 interrupted&quot;);
        }
    }
}

public class MainThread {

    public static void main(String[] args) {
        new SecondThread();
    
        try {
            for (int i=5; i&gt;0; i--) {
                System.out.println(&quot;main thread: &quot; + i);
                Thread.sleep(2000);
            }
        }
        catch (InterruptedException e) {
            System.out.println(&quot;main thread interrupted&quot;);
        }
    }
}
</code></pre>