2012년 11월 19일 월요일

Exception없이 Stack Trace를 출력하는 방법

일반적으로 stack trace를 출력하는 방법은 Exception을 이용하는 것이다.
하지만, 디버깅 중 Exception없이 stack trace를 확인하면 도움이 될 경우가 있다.

이럴 경우 아래 클래스를 이용하면 Exception없이도 stack trace를 출력할 수 있다.


public class StackTrace {

    public static String toStackTraceString() {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < elements.length; i++) {
            buf.append(elements[i].toString()).append('\n');
        }
        return buf.toString();
    }
}


댓글 없음: