package org.linlinjava.jz.hm.exception;
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(ele.getClassName()+ele.getMethodName()+ele.getFileName()+ele.getLineNumber()+"\n");
            }
            System.out.println(sbException);
        }
    }
}