[java基础]try catch 异常信息记录到日志文件

  1. 一定不要catch顶级异常类(Exception、Throwable),最外层调用者一定要处理这个异常。
  2. catch住的异常一定要记录到文件,方便以后问题排查
	public static String getTrace(Throwable t) {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        return buffer.toString();
    }
    logger.error(getTrace);

你可能感兴趣的:(Java,JavaWeb,异常,trycatch)