public static String getStackTrace(Throwable throwable) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { throwable.printStackTrace(pw); return sw.toString(); } finally { pw.close(); } }
测试
public static void test() { try { int i = 0; int m = 10 / i; } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("------调皮的分割线------"); System.out.println(ExceptionUtil.getStackTrace(e)); } }
原文:https://www.cnblogs.com/hyuxiang/p/12149011.html