Java異常堆棧字符串輸出

Java異常堆棧字符串輸出

public class ExceptionTrans {
    /**
     * 異常信息轉換爲字符串
     *
     * @param t 異常對象
     * @return
     */
    public static String ex2String(Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }

    public static void main(String[] args) {
        String ex = null;
        try {
            int a = 0;
            a = a / a;
            if (true) {
                throw new CordException("test");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ex = ex2String(e);
            System.out.println("------------------------");
        }
        System.out.printf(ex);
    }
}
相關文章
相關標籤/搜索