|Navigate Exceptions topic: )|
Stack Trace is a list of method calls from the point when the application was started to the point where the exception was thrown. The most recent method calls are at the top.
The stack trace can be printed to the standard error by calling the
public void printStackTrace method of an exception.
From Java 1.4, the stack trace is encapsulated into an array of a java class called
java.lang.StackTraceElement. The stack trace element array returned by
Throwable.getStackTrace method. Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated. Typically, this is the point at which the throwable corresponding to the stack trace was created.
A stack frame represents the following information:
|Code section 6.24: Stack frame.
1 public StackTraceElement(String declaringClass, 2 String methodName, 3 String fileName, 4 int lineNumber);
Creates a stack trace element representing the specified execution point.
Many times for debugging purposes, we'd like to convert the stack trace to a
String so we can log it to our log file.
The following code shows how to do that:
|Code section 6.25: Save the stack trace.
1 import java.io.StringWriter; 2 import java.io.PrintWriter; 3 4 ... 5 6 Exception e = new NullPointerException; 7 8 StringWriter outError = new StringWriter; 9 e.printStackTrace(new PrintWriter(outError)); 10 String errorString = outError.toString; 11 12 // Do whatever you want with the errorString
Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your Digital Marketing and Technology knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.