Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving Exception Full stacktrace

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

like image 512
phewataal Avatar asked May 11 '12 15:05

phewataal


1 Answers

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.

like image 107
Eugene Kuleshov Avatar answered Oct 11 '22 07:10

Eugene Kuleshov