java—Android---把完整的Exception堆栈输出到Log

有时候我们想吧Exception输出到logcat,但又不想出发程序的崩溃
用Log.e("",exception.toString)获取到的信息是不完整的,我们可以这样做:

Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
Throwable cause = e.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString();
Log.e(“result”,result)

你可能感兴趣的:(java,Android开发)