当前位置: 代码迷 >> J2SE >> e.getStackTrace()跟ExceptionUtils.getStackTrace(e)
  详细解决方案

e.getStackTrace()跟ExceptionUtils.getStackTrace(e)

热度:6847   发布时间:2013-02-25 00:00:00.0
e.getStackTrace()和ExceptionUtils.getStackTrace(e)?
哪位帮忙详细解释一下异常处理时,写日志使用
java api Exception中的getStackTrace()方法和org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e) 
两者有什么区别,对程序有什么影响?

------解决方案--------------------------------------------------------
没有区别。org.apache.commons.lang.exception.ExceptionUtils 这个类只不过是apache 封装过的。
------解决方案--------------------------------------------------------
正常情况下公司不会让你那么简单的就getStackTrace的
------解决方案--------------------------------------------------------
这两者倒是没什么区别
------解决方案--------------------------------------------------------
看了源码你就清楚了:
org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e):
Java code
public static String getStackTrace(Throwable throwable) {        StringWriter sw = new StringWriter();        PrintWriter pw = new PrintWriter(sw, true);        throwable.printStackTrace(pw);        return sw.getBuffer().toString();    }
------解决方案--------------------------------------------------------
楼主看下返回值,就知道为什么了。

e.getStackTrace() 返回一个 StackTraceElement[]

ExceptionUtils.getStackTrace(e) 返回一个 String

如果是打印日志,是不是直接输出一个String好呢?