Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM - Print Stack trace without explicit call

Is there a way in java to print a stack trace of any exception in a catch block without making code modifications. I was told that there was a JVM arg you could use to produce stack traces of all exceptions for debugging, although I can't find any documentation on this. The only solution I can think of for this is to use aspectj and create an aspect on any exception that is created and print the stack trace. I was hoping there was a better solution than aspects.

Thanks, Steve.

--Edit-- So what I want to find out is lets say I have this code: try { throw new Exception(); } catch (Exception e) { //Ignore exceptions }

I would like to see the e.printStackTrace() even though no call is made to it. This can help with debugging a jvm crash I am seeing, and there is a lot of error hiding going on.

like image 653
Steve Avatar asked May 21 '26 15:05

Steve


1 Answers

As Marko Topolnik said, logging any exception may take a bit of work, but you can also implement a custom uncaught exception handler to do whatever you please with uncaught exceptions.

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    private final Logger log = Logger.getLogger("EXCEPTION");

    public void uncaughtException(final Thread t, final Throwable e) {
        log.logp(Level.SEVERE, "EXCEPTION", "", "Unhandled exception in thread " + t.getName() + ": ", e);
    }
});
like image 108
FThompson Avatar answered May 23 '26 04:05

FThompson