使用String获取java异常中的异常内容

因为Exception类是所有异常的父类,所以使用它可以读取到java产生的所有异常:

    /**
     * @category 获取try-catch中的异常内容
     * @param e Exception
     * @return  异常内容
     */
    public static String getException(Exception e) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream pout = new PrintStream(out);
        e.printStackTrace(pout);
        String ret = new String(out.toByteArray());
        pout.close();
        try {
             out.close();
        } catch (Exception ex) {
            
        }
        return ret;
}


你可能感兴趣的:(使用String获取java异常中的异常内容)