what is stack trace in Java
Thread executes code in Java, they call methods, and when they call, they keep them in there stack memory. You can print that stack trace to find out, from where a particular method is get called in execution flow.
Thread executes code in Java, they call methods, and when they call, they keep them in there stack memory. You can print that stack trace to find out, from where a particular method is get called in execution flow.
One of the easiest way of printing stack trace of current thread in Java is by using dumpStack() method from java.lang.Thread class. This method prints stack trace of thread on which it get's called. You can use Thread.currentThread() method to get reference of current thread before calling this method
Another way printing stack trace is using printStackTrace() method of Throwable class
Main difference between using dumpStack() and printStackTrace() is first entry in Stack, In case of dumpStack() first entry is always java.lang.Thread.dumpStack(), while in later case it's the method from where you printed stack trace. If you don't want to print stack trace and rather wants it in Java program, you can use getStackTrace() method from Thread class. This method returns an array of StackTraceElement.
public class StackTraceExample { public static void main(String args[]) { //calling a method to print stack trace further down first(); } public static void first() { second(); } private static void second() { third(); } private static void third() { //If you want to print stack trace on console than use dumpStack() method System.err.println("Stack trace of current thread using dumpStack() method"); Thread.currentThread().dumpStack(); //This is another way to print stack trace from current method System.err.println("Printing stack trace using printStackTrace() method of Throwable "); new Throwable().printStackTrace(); //If you want stack trace as StackTraceElement in program itself than //use getStackTrace() method of Thread class StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); //Once you get StackTraceElement you can also print it to console System.err.println("displaying Stack trace from StackTraceElement in Java"); for(StackTraceElement st : stackTrace) { System.err.println(st); } } }Output:
Stack trace of current thread using dumpStack() method java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1206) at StackTraceExample.third(StackTraceExample.java:22) at StackTraceExample.second(StackTraceExample.java:15) at StackTraceExample.first(StackTraceExample.java:10) at StackTraceExample.main(StackTraceExample.java:6) Printing stack trace using printStackTrace() method of Throwable java.lang.Throwable at StackTraceExample.third(StackTraceExample.java:26) at StackTraceExample.second(StackTraceExample.java:15) at StackTraceExample.first(StackTraceExample.java:10) at StackTraceExample.main(StackTraceExample.java:6) displaying Stack trace from StackTraceElement in Java java.lang.Thread.getStackTrace(Thread.java:1436) StackTraceExample.third(StackTraceExample.java:29) StackTraceExample.second(StackTraceExample.java:15) StackTraceExample.first(StackTraceExample.java:10) StackTraceExample.main(StackTraceExample.java:6)
0 comments:
Post a Comment