title: How to make ScheduledThreadPool report errors?
body: <p>After a painful debugging experience, I tracked down this issue: <code>ScheduledThreadPool</code> does not report if a task fails, and does not execute a task that failed once ever again. Therefore, it is hard to keep track of the liveness of periodic jobs, short of checking them with yet other periodic tasks (via dead man's switch or the <code>ScheduledFuture</code>).</p>
  
  <p>Now we can hand a <code>ScheduledThreadPool</code> an <code>UncaughtExceptionHandler</code>, but not even that seems to work:</p>
  
  <pre><code>import java.util.concurrent.*;
  
  class Test {
    public static void main(String[] args) {
      final ThreadFactory tf = new ThreadFactory() {
        private final ThreadFactory delegate = Executors.defaultThreadFactory();
  
        @Override public Thread newThread(final Runnable r) {
          final Thread res = delegate.newThread(r);
          res.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(final Thread t, final Throwable e) {
              e.printStackTrace();
            }
          });
          return res;
        }
      };
      final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1, tf);
  
      final Runnable task = new Runnable() {
        private int c = 0;
  
        @Override
        public void run() {
          if ( c++ == 5 ) {
            throw new ArrayIndexOutOfBoundsException("Runtime error!");
          }
  
          System.out.println("Reached " + c);
        }
      };
  
      exec.scheduleWithFixedDelay(task, 1, 1, TimeUnit.SECONDS);
    }
  }
  </code></pre>
  
  <p>The output of this program is simply (Oracle Java SE (64-Bit Server) 1.7.0_06-b24)</p>
  
  <pre><code>Reached 1
  Reached 2
  Reached 3
  Reached 4
  Reached 5
  </code></pre>
  
  <p>and then it hangs (by design).</p>
  
  <p>I can always try-catch the whole task, but that feels ugly; the <code>UncaughtExceptionHandler</code> should do that already!</p>
  
  <p>Is there an API-solution for this issue? Did I do something wrong, or is it a bug?</p>