为什么我的UncaughtExceptionHandler没有捕获StackOverflowError?

时间:2014-12-18 12:35:09

标签: java stack-overflow uncaughtexceptionhandler

我正在运行Java守护程序并希望捕获未处理的异常。所以我写了一堂课:

public class CodeineUncaughtExceptionHandler 
    implements UncaughtExceptionHandler {

  private Logger log = Logger.getLogger(CodeineUncaughtExceptionHandler.class);
  private boolean errorPrintedToOut = false;

  public CodeineUncaughtExceptionHandler() {
  }

  @Override
  public void uncaughtException(Thread t, Throwable e) {
    try {
      if (!errorPrintedToOut) {
        errorPrintedToOut = true;
        e.printStackTrace();
      }
      log.error("Uncaught exception in thread " + t.getName(), e);
    } catch (Throwable tr) {
      try {
        e.printStackTrace();
        tr.printStackTrace();
      } catch (Throwable e1) {
        log.fatal("could not write stacktrace of exception " + t.getName());
        addExceptionInfo(e1);
        addExceptionInfo(e);
        addExceptionInfo(tr);
      }
    }
  }

  private void addExceptionInfo(Throwable e) {
    log.fatal("exception info " + e.getMessage() + " " + e);
  }
}

它的用法如下:

Thread.setDefaultUncaughtExceptionHandler(new CodeineUncaughtExceptionHandler());

出于某种原因,它似乎无法奏效。从文档中看,它应该应用于所有线程。但是我的输出文件缺少异常的完整堆栈跟踪:

  

异常:从线程" qtp18824904-18"

中的UncaughtExceptionHandler抛出java.lang.StackOverflowError

任何想法?

0 个答案:

没有答案