private static String getCrashMessage(Exception ex) {
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
return writer.toString();
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
return writer.toString();
}
可以通过该方法,将传入的exception统一管理,避免一些极端情况下,e.printStackTrace()出错。
版权声明:本文为qq_24179679原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。