The setStackTrace(StackTraceElement stackTrace) method of Throwable class is used to Set the stack trace elements to this throwable object and this stack trace will be returned by getStackTrace() and printed by printStackTrace() and related methods. This method allows the user to override the default stack trace that is either generated by fillInStackTrace() when a throwable is constructed or deserialized when a throwable is read from a serialization stream.
If the stack trace of any Throwable is not writable then calling this method has no effect other than validating its argument.
public void setStackTrace(StackTraceElement stackTrace)
Parameters: This method accepts only one parameter stackTrace which is the stack trace elements to be associated with this Throwable.
Returns: This method does not returns abything.
Below programs illustrate the setStackTrace() method:
StackTraceElement length :3 Stack Trace at index 0 : ClassName1.methodName1(fileName1:10) Stack Trace at index 1 : ClassName2.methodName2(fileName2:20) Stack Trace at index 2 : ClassName3.methodName3(fileName3:14)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.