如果你不仅想在日志中查看异常信息,也想把异常信息保存起来作为数据查看,那么你可以这样做
public void printException(Exception e) { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(arrayOutputStream)); BufferedReader fr = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(arrayOutputStream.toByteArray()))); String str; StringBuilder eStr = new StringBuilder(); try { while((str = fr.readLine()) != null){ eStr.append("\n"+str); } } catch (Exception e1) { e1.printStackTrace(); }finally { try { fr.close(); } catch (IOException e2) { e2.printStackTrace(); } } System.out.println(eStr.toString()); }
异常信息打印的出效果跟控制台看到的一致
标签:fr,java,str,打印,printStackTrace,eStr,new,异常,arrayOutputStream From: https://www.cnblogs.com/lccsdncnblogs/p/17130100.html