/** * 将 e.printStackTrace() 到控制台的信息,保存为一个 String * @param e * @return */ public static String getStackTraceAsString(Throwable e){ //StringWriter将包含堆栈信息 StringWriter stringWriter = new StringWriter(); //必须将StringWriter封装成PrintWriter对象,以满足printStackTrace的要求 PrintWriter printWriter = new PrintWriter(stringWriter); //获取堆栈信息 e.printStackTrace(printWriter); //转换成String,并返回该String return stringWriter.getBuffer().toString(); }