0

I am running a Java daemon and want to catch unhandled exceptions. So I wrote a class:

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);
  }
}

and it is used like this:

Thread.setDefaultUncaughtExceptionHandler(new CodeineUncaughtExceptionHandler());

For some reason it seems not to work. From the documentation it looks this should be applied to all threads. However my output file is missing the full stacktrace of the exception:

Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "qtp18824904-18"

Any Idea?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
oshai
  • 14,865
  • 26
  • 84
  • 140
  • Then the problem is not that it is not caught, is it? You should edit the title of the question – fge Dec 18 '14 at 12:37
  • I don't know if it caught or not. – oshai Dec 18 '14 at 12:44
  • Without any doubt, your exception handler **has worked** but has thrown a `StackOverflowError`, for some reason. Can you provide some self-contained example code that reproduces this issue? In principle your code works fine on my machine. – Duncan Jones Dec 18 '14 at 12:45
  • How can you tell whether or not your UEH is working? Add something to your UEH to let you know it is has done something. `print("In UEH.");` or whatever. – rossum Dec 18 '14 at 12:45
  • I dont have any - log.error entries so I assume uncaughtExceptionHandler is not called, but I am not sure. – oshai Dec 18 '14 at 12:48
  • @oshai Maybe the logging is related to your `StackOverflowError`. Try just printing to stdout to rule out logging issues. Basically, simplify your handler and increase its complexity until you spot what triggers the exception. – Duncan Jones Dec 18 '14 at 12:50
  • it might happen that your jetty's worker thread are started way before and blew up and then you added your handler. – SMA Dec 18 '14 at 12:50
  • @Duncan - I cant provide such code, I don't have an idea how to reproduce it. – oshai Dec 18 '14 at 12:57
  • @oshai So you've only seen this happen once? Or does it happen sporadically? You should change your title to "*Why is my UncaughtExceptionHandler throwing a StackOverflowError?*". – Duncan Jones Dec 18 '14 at 12:59
  • @Duncan - yes, it happens sporadically. I am willing to change the title If you are sure this is the cause. I thought maybe `Thread.setDefaultUncaughtExceptionHandler()` was called again after my code. is it possible? – oshai Dec 18 '14 at 13:06
  • @oshai Well, anything's possible. But a StackOverflowError has certainly been thrown from an uncaught exception handler. I think it's highly likely your own registered handler has thrown it. If in doubt, add `System.out.println("HARRRRR!");` as the first line in your handler. – Duncan Jones Dec 18 '14 at 13:09
  • Thanks, I added a print in the beginning of the handler. – oshai Dec 18 '14 at 13:32

0 Answers0