Skip to content
Search

Java.lang.StackTraceElement

lastmod 2020-01-14created 2020-01-141 edits

How to use

Use Thread to get the current Thread, then get the Stack Trace from that Thread.

Thread th = Thread.currentThread();

You can receive it using a StackTraceElement array.

StackTraceElement[] lists = th.getStackTrace();

Then print what you want using the methods of StackTraceElement.

// Print StackTrace...
Thread th = Thread.currentThread();
StackTraceElement[] lists = th.getStackTrace();
for (StackTraceElement list : lists) {
    Log.w("StackTraceElement", list.toString());
    // list.getClassName();
    // list.getMethodName();
}

Example

public class StackTraceElementTest {
    public void test1(){
        test2();
    }

    public void test2(){
        test3();
    }

    public void test3(){
        test4();
    }

    public void test4(){
        Thread th = Thread.currentThread();
        StackTraceElement[] lists = th.getStackTrace();
        for(StackTraceElement list : lists){
            System.out.println(list);
            System.out.println(list.getClassName() + " : " + list.getMethodName());
        }
    }

    public static void main(String[] args){
        StackTraceElementTest test = new StackTraceElementTest();
        test.test1();
    }
}

See also

Favorite site

author Zer0 sitemap