retrieving Exception Full stacktrace

phewataal picture phewataal · May 11, 2012 · Viewed 7.4k times · Source

I am trying to upgrade Apache commons lang from 2.4 to 3.1 in my project. My implementation has a reference to ExceptionUtil.getFullExceptionTrace(e) which does not exist anymore in 3.1. The reason of removal was listed as

"Removing isThrowableNested, isNestedThrowable and getFullStackTrace as they were all types of no-op once you got to JDK 1.4. LANG-491"

. Few questions around this change:

  1. I am confused if this means we need to explore some other way to retrieve full stack trace or we can simply replace with ExceptionUtils.getStackTrace(e) instead.

  2. Any idea on what changed after jdk 1.4 to make the method redundant?

  3. Can't we simply do e.toString() where e is my Exception instance?

Thanks

Answer

Eugene Kuleshov picture Eugene Kuleshov · May 11, 2012

Java 1.4 introduced getCause() method on Throwable class which is a parent for all Exception classes. If you need to get a stack trace as a String, you can use ExceptionUtils.getStackTrace(e) or something like this:

  Exception e = ...
  StringWriter sw = new StringWriter();
  e.printStackTrace(new PrintWriter(sw));
  String stack Trace = sw.toString();

See additional discussion about removing methods from ExceptionUtil on JIRA issue LANG-491.