Java 打印堆栈的几种方法 Exception

Exception e = new Exception("this is a log");
e.printStackTrace();
//延迟才可以看出效果
Thread.currentThread().sleep(1000);
System.out.println("-------------以上是异常详细信息----------------");


System.out.println("打印异常1:"+e.toString());
String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);
System.out.println("打印异常2:"+fullStackTrace);

 

package name.xu;
public class CallStack {
    public static void printCallStatck() {
        Throwable ex = new Throwable();
        StackTraceElement[] stackElements = ex.getStackTrace();
        if (stackElements != null) {
            for (int i = 0; i < stackElements.length; i++) {
                System.out.print(stackElements[i].getClassName()+"/t");
                System.out.print(stackElements[i].getFileName()+"/t");
                System.out.print(stackElements[i].getLineNumber()+"/t");
                System.out.println(stackElements[i].getMethodName());
                System.out.println("-----------------------------------");
            }
        }
    }
    
}

 

posted @ 2016-09-05 17:50  myJoan  阅读(13655)  评论(0编辑  收藏  举报