title: Do errors thrown within UncaughtExceptionHandler get swallowed?
body: <p><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.html#uncaughtException(java.lang.Thread,%20java.lang.Throwable)" rel="nofollow"><code>Thread.UncaughtExceptionHandler</code></a> states that when the method which handles uncaught exceptions itself throws an exception, that exception will be ignored:</p>
  
  <blockquote>
    <p>void <strong>uncaughtException</strong>(Thread t, Throwable e):</p>
    
    <p>Method invoked when the given thread terminates due to the given
    uncaught exception.</p>
    
    <p><strong>Any exception thrown by this method will be ignored by the Java
    Virtual Machine.</strong></p>
  </blockquote>
  
  <p>However when I tested it, the JVM did not ignore the exceptions handled by the uncaught exception handler`:</p>
  
  <pre><code>public static void main(final String args[]) {
      Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread arg0, Throwable arg1) {
              throw new java.lang.RuntimeException("e2");
          }
      });
      throw new RuntimeException("e1");
  }
  </code></pre>
  
  <p>Eclipse Console output (JRE 1.7):</p>
  
  <blockquote>
    <p>Exception: java.lang.RuntimeException thrown from the
    UncaughtExceptionHandler in thread "main"</p>
  </blockquote>
  
  <p>Another oddity I found out is that the output I get isn't coming from <code>System.err</code>. It seems to be from another stream altogether. I verified this by redirecting <code>System.err</code> to <code>System.out</code>, but I'm still getting "red" output:</p>
  
  <pre><code>public static void main(final String[] args) {
      System.setErr(System.out);
      System.out.println(System.err == System.out);
      System.err.println("this is black color");
      try {
          throw new Error("test stacktrace color");
      } catch (Throwable e) {
          e.printStackTrace();
      }
      try {
          Thread.sleep(2500);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread t, Throwable e) {
              throw new RuntimeException("from handler");
          }
      });
      throw new RuntimeException("from main");
  }
  </code></pre>
  
  <p>The output (bolded signifies red color):</p>
  
  <blockquote>
    <p>true</p>
    
    <p>this is black color</p>
    
    <p>java.lang.Error: test stacktrace color at asf.df.main(df.java:13)</p>
    
    <p><strong>Exception: java.lang.RuntimeException thrown from the UncaughtExceptionHandler in thread "main"</strong></p>
  </blockquote>
  
  <p>What's the explanation for these phenomenons?</p>
  
  <p>What happens to errors thrown within UncaughtExceptionHandler? What's the expected (documented or guaranteed) behavior?</p>