某些情况下,我们需要获取应用打印的异常信息,这时就可以借助StringWriter和PrintWriter两个类来获取异常信息,具体用法如下:

try{
throw new NullPointerException();
}catch (Exception e){
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw,true));
String infomsg = sw.toString();
System.out.println(infomsg);
}