Java开发中,遇到异常时,我们需要将异常的详细信息写入日志,以备查看。
public class ExceptionUtil {
public static String getStackTrace(Throwable exp) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
exp.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
}