My first question is who actually prints the prints the stack trace ? Is it the JVM or some other entity? What happens behind the scene when stack trace is printed ? I am looking specifically for an answer that tells me the role of Uncaught exception handlers in printing the stacktrace if there is an application provided UncaughtExceptionhandler.
Share
Are you talking about uncaught exceptions? If you catch an exception you can print the stack trace yourself using the
Throwable.printStackTracemethod. If you want to know how exactly it prints the stack trace you can have a look at the source code.For uncaught exceptions, the stack trace is printed by the “uncaught exception handler” of the thread where the exception is thrown. If the thread does not have an uncaught exception handler of its own the thread group’s handler is used, which in turn delegates to the parent thread group or to the “default uncaught exception hander.” If there is no parent nor a default handler, this happens:
You can install your own uncaught exception handler using
Thread.setUncaughtExceptionHandler. To set a the default uncaught exception handler to catch any uncaught exceptions useThread.setDefaultUncaughtExceptionHandler