2

The environment is a Java program deployed in WildFly 11.0.0.Final and a terminal window that started standalone.sh and remains open showing the output.

Explicit output such as PrintStream#println and Throwable#printStackTrace is printed as expected, but when runtime exceptions such as ArrayIndexOutOfBoundsException are thrown which naturally are not caught and thus not explicitly printed, nothing is printed. Right now I am forced to catch runtime exceptions and print them, which obviously is bothersome.

Is there a way to make WildFly print the stack trace of runtime exceptions automatically as one would expect it to do?

RaminS
  • 2,208
  • 4
  • 22
  • 30
  • Are the uncaught exceptions being written to a log? What does your logging configuration look like? – Jim Garrison Jan 04 '18 at 06:31
  • @JimGarrison I have not configured any logging. As far as I can tell there is only the default `server.log` which shows the same thing as the terminal output. – RaminS Jan 04 '18 at 06:33
  • 1
    What are you developing? I've seen the type of behavior you're seeing with JAX-RS - it wants to return a 500 to the front end and it ends up not logging quite the way I want. – stdunbar Jan 04 '18 at 22:02
  • @stdunbar I am using Java WebSockets. The runtime exceptions are thrown inside [`onMessage`](https://docs.oracle.com/javaee/7/api/javax/websocket/MessageHandler.Whole.html#onMessage-T-). – RaminS Jan 05 '18 at 07:09
  • And your code looks like? – stdunbar Jan 06 '18 at 19:01

1 Answers1

0

Use a Thread.UncaughtExceptionHandler

Thread thread = new Thread() {
    public void run() {
        System.out.println("Thread start");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Throw exception");
        throw new RuntimeException();
    }
};

Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("Uncaught thread " + t + " exception: " + e);
    }
};

thread.setUncaughtExceptionHandler(handler);
thread.start();
Anton Tupy
  • 951
  • 5
  • 16
  • This is a workaround rather than a solution. Runtime exceptions won't exist in production either way, and during development it is sufficient to wrap everything in one big try-catch if you are looking for a workaround I think. I am looking for a way to configure WildFly to print the stack traces as one would expect it to do. – RaminS Jan 04 '18 at 07:51