---
title: "Exception Handling in Java: Printing a Stack Trace"  
description: "In this post we are going to learn a very common practice developers usually follow when they are dealing with Exception handling. There is very handy"  
author: "Anonymous User"  
published: 2016-05-21  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11117/exception-handling-in-java-printing-a-stack-trace  
category: "java"  
tags: ["exception handling", "java"]  
reading_time: 4 minutes  

---

# Exception Handling in Java: Printing a Stack Trace

In this post we are going to learn a very common practice [developers](https://www.mindstick.com/blog/302688/handling-errors-in-rust-a-comprehensive-guide-for-developers) usually follow when they are dealing with [Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling). There is very handy method provided by java called printStackTrace, [as the name](https://www.mindstick.com/forum/34352/the-term-update-database-is-not-recognized-as-the-name-of-a-cmdlet-function) suggest this method help us to track the method stack from where the exception is originated.

In some of the earlier examples in Exception handling, we might have noticed that in the exception handler we used the following statement:

e.printStackTrace();

This statement prints a stack dump to the user console. Although, typically, this is used for getting the stack dump in case of errors, it can also be used in other situations where there are no errors.

With J2SE 1.4 onward, this better way of analyzing a stack trace is available. For example, when we have a [recursive function](https://www.mindstick.com/forum/156808/write-a-program-for-making-recursive-function-in-any-language) call in our program, such as a program that generates [Fibonacci](https://www.mindstick.com/forum/34692/how-to-generate-fibonacci-triangle-in-c-sharp) numbers, we may want to study:

· How many times and

· When the recursive method is called.

To get a stack trace, we construct a **Throwable** object and call its getStackTrace method.

##### This is illustrated in the program given here:

```
public class FibonacciGenerator {
                     public static void main(String[] args) {                             generate(3);                     }
                     public static int generate(int n) {
                             Throwable t = new Throwable();                             StackTraceElement[] frames = t.getStackTrace();                             for (StackTraceElement frame : frames) {                                      System.out.println("Calling: " + frame.getMethodName());
                             }                             if (n <= 2) {
                                      return 1;                             } else {
                                      return generate(n - 1) + generate(n - 2);                             }
                     }
}
```

The program illustrated above computes and prints a Fibonacci number for a specified input.

The recursive function generate does this. In each call to the function, it obtains a stack trace and prints the called [method name](https://www.mindstick.com/forum/33583/selector-creation-with-method-name-and-parameter) to the console. The **getStackTrace** [method returns](https://www.mindstick.com/forum/12904/main-method-returns-if-submethod-has-stoped-by-return-in-c-sharp) an array of **StackTraceElements**. The program iterates through this array to print the [method names](https://www.mindstick.com/forum/157800/what-happens-in-case-of-the-inherited-interfaces-have-conflicting-method-names) pushed on the stack.

##### Output:

The program output is shown for the case when the Fibonacci number for value 3 is computed:

```
Calling: generate
Calling: main
Calling: generate
Calling: generate
Calling: main
Calling: generate
Calling: generate
Calling: main
```

Note that the generate function calls itself twice recursively. Therefore, we will see multiple calls to generate between every two calls to the main function. Thus, using this stack trace facility, we can get the stack dump anytime in our program and we do not have to necessarily wait for an exception to occur in the code.

In J2SE 5.0 onward, we can obtain a stack trace of all the running threads by calling the **getAllStackTraces** [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp) of the Thread class.

---

Original Source: https://www.mindstick.com/blog/11117/exception-handling-in-java-printing-a-stack-trace

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
