Java异常堆栈字符串输出
public class ExceptionTrans {
/**
* 异常信息转换为字符串
*
* @param t 异常对象
* @return
*/
public static String ex2String(Throwable t) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw, true));
return sw.getBuffer().toString();
}
public static void main(String[] args) {
String ex = null;
try {
int a = 0;
a = a / a;
if (true) {
throw new CordException("test");
}
} catch (Exception e) {
e.printStackTrace();
ex = ex2String(e);
System.out.println("------------------------");
}
System.out.printf(ex);
}
}