try { .... } catch (Exception e) { e.printStackTrace(); log.err(e.getMessage()); ..... }
通常我们都会去这样找到出错的信息,而打印的出错的信息栈,因为会输出到std.err中,所以在我们自己定义的日志文件中是不能够找到的,为了解决这个问题,可以通过如下代码解决:
public static String errInfo(Exception e) { StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw); // 将出错的栈信息输出到printWriter中 e.printStackTrace(pw); pw.flush(); sw.flush(); } finally { if (sw != null) { try { sw.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (pw != null) { pw.close(); } } return sw.toString(); }
e.getMessage(); 只会获得具体的异常名称. 比如说NullPoint 空指针,就告诉你说是空指针
e.printStackTrace();会打出详细异常,异常名称,出错位置,便于调试用