Java:打印異常信息與調用堆棧到控制檯

Java語言的異常類Exception包含着異常的所有信息。java

現設異常的實例名爲ex,全部的異常都是一個除以0的表達式(int i = 1 / 0)所觸發的:app

一、經過ex.getMessage()能夠獲取異常信息,如函數

/ by zero

二、經過ex.toString()能夠獲取異常類型和異常信息,如測試

java.lang.ArithmeticException: / by zero

三、經過ex.printStackTrace()能夠直接在控制檯打印異常的所有信息(包括堆棧),但該函數最好不要直接調用無參數的版本,由於這樣會讓控制檯顯示出現錯亂。調用printStackTrace時,經過以下方法調用:ui

StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
ex.printStackTrace(printWriter);
System.out.println(stringWriter.toString());

使用此方法,控制檯輸出的內容爲:code

java.lang.ArithmeticException: / by zero
	at ExceptionTest.exec3(ExceptionTest.java:31)
	at ExceptionTest.exec2(ExceptionTest.java:23)
	at ExceptionTest.exec1(ExceptionTest.java:15)
	at ExceptionTest.main(ExceptionTest.java:39)

若是使用的是Eclipse的控制檯,異常類名和指明行數的錯誤堆棧都會生成相應的跳轉連接。orm

四、經過ex.getStackTrace()能夠獲取堆棧信息,這是 ex.printStackTrace() 外的另外一種獲取異常堆棧信息的方法。經過此方法能夠定製化輸出堆棧信息。ip

System.out.println(ex.getClass().getName() + ": " + ex.getMessage());
StringBuilder sbException = new StringBuilder();
for (StackTraceElement ele : ex.getStackTrace()) {
    sbException.append(MessageFormat.format("\tat {0}.{1}({2}:{3})\n", 
        ele.getClassName(), ele.getMethodName(), ele.getFileName(), ele.getLineNumber()));;
}
System.out.println(sbException);

打印出的異常堆棧爲:get

java.lang.ArithmeticException: / by zero
	at ExceptionTest.exec3(ExceptionTest.java:31)
	at ExceptionTest.exec2(ExceptionTest.java:23)
	at ExceptionTest.exec1(ExceptionTest.java:15)
	at ExceptionTest.main(ExceptionTest.java:39)

最後列一下完整的Java代碼:string

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;

/**
 * 異常打印測試
 * @文件名稱 ExceptionTest.java
 * @文件做者 Tsybius2014
 * @建立時間 2016年5月13日 下午6:14:08
 */
public class ExceptionTest {
    /** 第一層函數 */
    public static void exec1() {
        try {
            exec2();
        } catch (Exception ex) {
            throw ex;
        }
    }
    /** 第二層函數 */
    public static void exec2() {
        try {
            exec3();
        } catch (Exception ex) {
            throw ex;
        }
    }
    /** 第三層函數 */
    public static void exec3() {
        try {
            int i = 1 / 0;
        } catch (Exception ex) {
            throw ex;
        }
    }
    /** 主函數 */
    public static void main(String[] args) {
        try {
            exec1();
        } catch (Exception ex) {
            System.out.println("--- getMessage ---");
            System.out.println(ex.getMessage());
            System.out.println();
            System.out.println("--- toString ---");
            System.out.println(ex.toString());
            System.out.println();
            System.out.println("--- printStackTrace ---");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            System.out.println(stringWriter.toString());
            System.out.println();
            System.out.println("--- printStackTrace DIY ---");
            System.out.println(ex.getClass().getName() + ": " + ex.getMessage());
            StringBuilder sbException = new StringBuilder();
            for (StackTraceElement ele : ex.getStackTrace()) {
                sbException.append(MessageFormat.format("\tat {0}.{1}({2}:{3})\n", 
                    ele.getClassName(), ele.getMethodName(), ele.getFileName(), ele.getLineNumber()));;
            }
            System.out.println(sbException);
        }
    }
}

控制檯輸出結果以下:

END

相關文章
相關標籤/搜索