title: Why do I need to handle an exception for Thread.sleep()?
body: <p>To get this code to compile, I can either:</p>
  
  <ul>
  <li>Put my call to <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep%28long%29" rel="noreferrer"><code>Thread.sleep()</code></a> in a try/catch block, or</li>
  <li>Have <code>printAll()</code> declare that it can throw an <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html" rel="noreferrer"><code>InterruptedException</code></a>.</li>
  </ul>
  
  <p>Why do I have to do this?</p>
  
  <pre><code>class Test {
      public static void main( String[] args ) {
            printAll( args );
      }
  
      public static void printAll( String[] line ) {
          System.out.println( lines[ i ] );
          Thread.currentThread().sleep( 1000 ):
      }
  }
  </code></pre>
  
  <p>(Sample code from <a href="https://rads.stackoverflow.com/amzn/click/com/0071591060" rel="noreferrer" rel="nofollow noreferrer">Kathy Sierra's SCJP book</a>.)</p>
  
  <p>I know that the exception which <code>Thread.sleep()</code> throws is a checked exception, so I have to handle it, but in what situation does <code>Thread.sleep()</code> need to throw this exception?</p>