Arafe Zawad Sajid Arafe Zawad Sajid - 11 months ago 51
Java Question

How does the getStackTrace() method work in java?

The getStackTrace() method returns an array of StackTraceElement[]. My question is, during exception handling how does it return the array? 1)By creating the array when an exception occurs (array created once while exception occurs) and then returning the array? 2)Or, is the array created each time the method is called (array created several times) and then returned?

Here I want to know how the method works. This is not related to the question: Are stack traces generated when a Java exception is thrown?

Answer Source

It is hard to answer because answer may be platform dependent.

But using common sense I can't find any reason to create stack trace every time some method is invoked (It will consume memory and take time to create it without any visible benefit most of the time.)