Are stack traces created when a Java exception is thrown?

This assumes that we are not calling the .printstacktrace method - just throw and catch.

We consider this for some performance bottlenecks.

+6
java performance exception exception-handling jvm
source share
4 answers

Yes. Throwable () constructors call fillInStackTrace (). (At least in Sun / Oracle JDK 6 for Windows.)

+10
source share

Stacktrace is captured when an exception is thrown.

If you really don't care about stacktrace, you can throw an exception once and throw it several times, but it looks like a hack and can be misleading.

+6
source share

A Throwable object grabs the current stack (using native code) if it will be printed or not. This is the reason why Exceptions should not be (ab) used for control flow.

+4
source share

it is not created lazily when the printStackTrace () method is called

Neal Gafter (former Sun engineer on the Java team) mentioned the effectiveness of exceptions here :

The most expensive part of an exception is handling far - stack capture trace when creating an exception

Also see this question .

+2
source share

All Articles