title: Compilation error:Exception is never thrown in body of corresponding try statement
body: <p>In my quest to learn Java better, I have been trying to understand exception handling.   I cannot understand why the following code fails to compile.   </p>
  
  <p>The compiler message is:</p>
  
  <pre><code>TestExceptionHandling.java:12: error: exception StupidException is never thrown in body of corresponding try statement
                  catch (StupidException stupidEx) {
                  ^
  </code></pre>
  
  <p>In the <code>try</code> block, method exTest.doExTest() is called.   In this method I catch an InterruptedException and in its <code>catch</code> block I throw a new StupidException.</p>
  
  <p>So why does the compiler say it's not thrown?   What am I missing?   Can any expert help me see my mistake please?</p>
  
  <pre><code>  public class TestExceptionHandling {
  
      public static void main(String argv[]) {
  
          String output = "";
  
          try {
              output = "\nCalling method doExTest:\n";
              exTest.doExTest();
          }
  
          catch (StupidException stupidEx) {
              System.out.println("\nJust caught a StupidException:\n   " + stupidEx.toString());
          }
  
          System.out.println(output);
      }   
  }   
  
  class exTest {
  
      static long mainThreadId;
  
      protected static void doExTest() {  //throws StupidException 
  
          mainThreadId = Thread.currentThread().getId();
          Thread t = new Thread(){
              public void run(){
                  System.out.println("Now in run method, going to waste time counting etc. then interrupt main thread.");
                  // Keep the cpu busy for a while, so other thread gets going...
                  for (int i = 0; i &lt; Integer.MAX_VALUE; i++) {
                      int iBoxed = (int)new Integer(String.valueOf(i));
                      String s = new String("This is a string" + String.valueOf(iBoxed));
                  }   
                  // find thread to interrupt...
                  Thread[] threads = new Thread[0];
                  Thread.enumerate(threads);
                  for (Thread h: threads) { 
                      if (h.getId() == mainThreadId) {
                          h.interrupt();
                      }
                  }
              }
          };
  
          t.start();
  
          try {
              Thread.sleep(5000);
          }
  
          catch (InterruptedException e){
              System.out.println("\nAn InterruptedException " + e.toString() + " has occurred.   Exiting...");
              throw new StupidException("Got an InterruptedException ", e); // ("Got an InterruptedException.   Mutated to StupidException and throwing from doExTest to caller...", e);
          }   
  
      }
  }
  
  class StupidException extends Exception {
  
      public StupidException(String message, Throwable t) {
          super(message + " " + t.toString());
      }
  
      public String toString() {
          return "Stupid Exception:  " + super.toString();
      }
  }
  </code></pre>