Home > DeveloperSection > Blogs > Exception Handling in Java: Printing a Stack Trace

Exception Handling in Java: Printing a Stack Trace


Java Exception Handling  Java 
Ratings:
0 Comment(s)
 235  View(s)
Rate this:

Exception Handling in Java: Printing a Stack Trace


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 method provided by java called printStackTrace, as the name 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 call in our program, such as a program that generates Fibonacci 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 to the console. The getStackTrace method returns an array of StackTraceElements. The program iterates through this array to print the 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 of the Thread class.


Don't want to miss updates? Please click the below button!

Follow MindStick