捕捉Java的异常Stacktrace

    public static String exceptionToString(Throwable th) {
        String stTrace = "";
        StringWriter sout = null;
        PrintWriter out = null;
        try {
            sout = new StringWriter();
            out = new PrintWriter(sout);
            th.printStackTrace(out);

            stTrace = sout.toString();
        } catch (Exception ex) {
        } finally {
            try {
                if (out != null)
                    out.close();
                if (sout != null)
                    sout.close();
            } catch (IOException ex) {
            }
        }
        return stTrace;
    }

猜你喜欢

转载自blog.csdn.net/rdsuncn1977/article/details/54754918