title: How to catch exceptions in FutureTask
body: <p>After finding that <code>FutureTask</code> running in a <code>Executors.newCachedThreadPool()</code> on Java 1.6 (and from Eclipse) swallows exceptions in the <code>Runnable.run()</code> method, I've tried to come up with a way to catch these without adding throw/catch to all my <code>Runnable</code> implementations.</p>
  
  <p>The API suggests that overriding <code>FutureTask.setException()</code> should help in this:</p>
  
  <blockquote>
    <p>Causes this future to report an ExecutionException  with the given throwable as its cause, unless this Future has already been set or has been cancelled. This method is invoked internally by the run method upon failure of the computation. </p>
  </blockquote>
  
  <p>However this method doesn't seem to be called (running with the debugger shows the exception is caught by <code>FutureTask</code>, but <code>setException</code> isn't called). I've written the following program to reproduce my problem:</p>
  
  <pre><code>public class RunTest {
      public static void main(String[] args) {
          MyFutureTask t = new MyFutureTask(new Runnable() {
  
              @Override
              public void run() {
                  throw new RuntimeException("Unchecked exception");
  
              }
          });
  
          ExecutorService service = Executors.newCachedThreadPool();
          service.submit(t);
      }
  }
  
  public class MyFutureTask extends FutureTask&lt;Object&gt; {
  
      public MyFutureTask(Runnable r) {
          super(r, null);
      }
  
      @Override
      protected void setException(Throwable t) {
          super.setException(t);
          System.out.println("Exception: " + t);
      }
  }
  </code></pre>
  
  <p>My main question is: How can I catch Exceptions thrown in a FutureTask? Why doesn't <code>setException</code> get called?</p>
  
  <p>Also I would like to know why the <code>Thread.UncaughtExceptionHandler</code> mechanism  isn't used by <code>FutureTask</code>, is there any reason for this?</p>