Stack Trace

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.

Computer code Code listing 6.3:
 1 public class StackTraceExample {
 2   public static void main(String[] args) {
 3     method1;
 4   }
 6   public static void method1 {
 7     method11;
 8   }
10   public static void method11 {
11     method111;
12   }
14   public static void method111 {
15     throw new NullPointerException("Fictitious NullPointerException");
16   }
17 }
Standard input or output Output for Code listing 6.3
Exception in thread "main" java.lang.NullPointerException: Fictitious NullPointerException
at StackTraceExample.method111(
at StackTraceExample.method11(
at StackTraceExample.method1(
at StackTraceExample.main(

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:

Example 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.

Converting the stack trace into string

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:

Example Code section 6.25: Save the stack trace.
 1 import;
 2 import;
 4 ...
 6   Exception e = new NullPointerException;
 8   StringWriter outError = new StringWriter;
 9   e.printStackTrace(new PrintWriter(outError));
10   String errorString = outError.toString;
12   // Do whatever you want with the errorString


To do:
Add some exercises like the ones in Variables

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at Create an account using LinkedIn to manage and organize your omni-channel knowledge. is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us