转自:http://blog.youkuaiyun.com/long95wang/article/details/8089489
方法一:
public static String getExceptionAllinformation(Exception ex){
String sOut = “”;
StackTraceElement[] trace = ex.getStackTrace();
for (StackTraceElement s : trace) {
sOut += “\tat ” + s + “\r\n”;
}
return sOut;
}
方法二:
public static String getExceptionAllinformation_01(Exception ex) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream pout = new PrintStream(out);
ex.printStackTrace(pout);
String ret = new String(out.toByteArray());
pout.close();
try {
out.close();
} catch (Exception e) {
}
return ret;
}
方法三:
private static String toString_02(Throwable e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
本文介绍了三种获取Java异常详细信息的方法,包括使用StackTraceElement遍历堆栈跟踪、将异常信息输出到ByteArrayOutputStream以及利用StringWriter捕获异常详情。这些方法有助于开发者更有效地定位和解决问题。
2658

被折叠的 条评论
为什么被折叠?



