Geek
Geek

Reputation: 27193

What is the role of an application provided uncaught exception handler in printing the stack trace?

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.

Upvotes: 3

Views: 163

Answers (3)

Joni
Joni

Reputation: 111259

Are you talking about uncaught exceptions? If you catch an exception you can print the stack trace yourself using the Throwable.printStackTrace method. 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:

Otherwise, this method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.

You can install your own uncaught exception handler using Thread.setUncaughtExceptionHandler. To set a the default uncaught exception handler to catch any uncaught exceptions use Thread.setDefaultUncaughtExceptionHandler

Upvotes: 1

A Throwable and all its sub-classes like Exception and RuntimeException is required by the try-catch and throw keywords, and it is the Java part of the JVM which handles them.

The only thing not pure Java is the fillInStackTrace() method being native meaning that this method is provided by the JVM "outside" the Java part. You can inspect all parts of the stack trace if you have a Throwable in a variable.

http://www.docjar.com/html/api/java/lang/Throwable.java.html

Note that in "the old days" Exceptions were expensive objects (I believe it was because the stacktrace was computed when the exception was constructed, regardless if it was to be used or not) but this has been optimized quite a bit by delaying the stack trace generation until it is actually needed.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

1. When an exception occurs it is thrown to the JVM.

2. JVM then prints the StackTrace.

3. StackTrace includes the Method name, Class name, File name, and the Line number.

4. fillInStackTrace() method is called to re-initialize the stack trace data in the newly created throwable. Will be helpful in masking the info about the exception when tries to access the API.

Upvotes: 3

Related Questions