通过Exception获取其中的信息

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();

    }

以上为转载文章:地址:https://blog.csdn.net/qq_24179679/article/details/79017551

以下为项目中使用记录:

try {
xxxxxx
} catch (Exception e) {
Throwable cause = e.getCause();
while (cause != null) {
System.err.println(cause);
cause = cause.getCause();
}
StackTraceElement[] stackTraceElements = e.getStackTrace();
for (StackTraceElement stackTraceElement : stackTraceElements) {
if (stackTraceElement.toString() != null && stackTraceElement.toString().startsWith("cn.com.xxx.xxx")) {
System.err.println(stackTraceElement.toString());//打印错误位置
}
}
}

posted @ 2018-07-06 18:23  白天過後的黑夜  阅读(276)  评论(0编辑  收藏  举报