Skip to content

Java.lang.StackTraceElement

How to use

Thread를 이용하여 현재 Thread를 얻어오고, 얻어온 Thread로부터 Stack Trace를 가져온다.

Thread th = Thread.currentThread();

StackTraceElement 배열을 이용하면 받을 수 있게 된다.

StackTraceElement[] lists = th.getStackTrace();

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