Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are stack traces generated when a Java exception is thrown?

This is assuming we don't call the .printstacktrace method - just throwing and catching.

We are considering this for some performance bottlenecks.

like image 486
Something else Avatar asked Nov 16 '10 14:11

Something else


People also ask

How are stack traces generated?

A stack trace is generated whenever your app crashes because of an error or an exception. You can also print a stack trace at any point in your app code using methods such as Thread.

What happens when an exception is thrown Java?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

What is exception stack trace in Java?

A Java stack trace is displayed when an error or exception occurs. The stack trace, also called a backtrace, consists of a collection of stack records, which store an application's movement during its execution.

How does stack trace work in Java?

Simply put, a stack trace is a representation of a call stack at a certain point in time, with each element representing a method invocation. The stack trace contains all invocations from the start of a thread until the point it's generated. This is usually a position at which an exception takes place.


1 Answers

No, stack traces are generated when exception object is constructed, not when it's thrown. The Throwable() constructors call fillInStackTrace(). (At least in Sun/Oracle's JDK 6 for Windows.)

like image 55
perp Avatar answered Oct 03 '22 20:10

perp