Posted By

maxkar on 10/31/08


Tagged

java exception stacktrace


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

liuhu_snipplr
studioevoque


stackTraceUtil


 / Published in: Java
 

  1. import java.io.PrintWriter;
  2. import java.io.StringWriter;
  3. import java.io.Writer;
  4. import org.apache.commons.lang.exception.ExceptionUtils;
  5.  
  6. public class StackTraceUtil {
  7.  
  8. public static String getStackTrace(Throwable aThrowable) {
  9. final Writer result = new StringWriter();
  10. final PrintWriter printWriter = new PrintWriter(result);
  11. aThrowable.printStackTrace(printWriter);
  12. return result.toString();
  13. }
  14.  
  15. public static String getRootCauseStackTrace(Throwable aThrowable) {
  16. final Writer result = new StringWriter();
  17. final PrintWriter printWriter = new PrintWriter(result);
  18. ExceptionUtils.printRootCauseStackTrace(aThrowable, printWriter);
  19. return result.toString();
  20. }
  21.  
  22. public static String getFriendlyStackTrace(Throwable aThrowable) {
  23. final StringBuilder result = new StringBuilder("StackTrace:\n");
  24. final String NEW_LINE = "\n\n";
  25.  
  26. for (Object object : ExceptionUtils.getThrowableList(aThrowable)) {
  27. Exception ex = (Exception)object;
  28. if ( null != ex.getClass())
  29. {
  30. result.append( ex.getClass()+": " );
  31. result.append( ex.getMessage());
  32. result.append( NEW_LINE );
  33. }
  34. }
  35. return result.toString();
  36. }
  37. }

Report this snippet  

You need to login to post a comment.