Saturday 19 July 2014

Get current stack trace in Java

By using Thread.currentThread().getStackTrace(), we can get the current stack trace.

public class CurrentStackTrace {
    
    static void a(){
        b();
    }
    
    static void b(){
        c();
    }
    
    static void c(){
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        for (StackTraceElement ste : trace) {
            System.out.println(ste);
        }
    }
    
    public static void main(String args[]){
        a();
    }
}

Output
java.lang.Thread.getStackTrace(Thread.java:1552)
CurrentStackTrace.c(CurrentStackTrace.java:13)
CurrentStackTrace.b(CurrentStackTrace.java:9)
CurrentStackTrace.a(CurrentStackTrace.java:5)
CurrentStackTrace.main(CurrentStackTrace.java:20)




                                                             Home

No comments:

Post a Comment