输出异常信息

Writer out = new StringWriter();
PrintWriter s = new PrintWriter(out); // 创建不带自动行刷新的新 PrintWriter。
exception.printStackTrace(s); //将异常信息放入out中
msg = out.toString();

 

exceoption的printStackTrace(PrintWriter p) 从Throwable 继承来(public class Exception extends Throwable )

将异常输出到p中。参数只有PrintWriter类型,所以将out转成PrintWriter,用StringWriter的toString则能获取异常信息到字符串中

posted @ 2018-10-28 12:01  圣金巫灵  阅读(372)  评论(0编辑  收藏  举报