Exception stackTrace to a String Variable

There is an easy an elegant way to convert or store an exception stack trace to a string variable. The following method takes an exception object as a parameter and returns the string representation of the stack trace.

public String getStackTrace(Exception ex) {
        java.io.StringWriter out = new java.io.StringWriter();
        ex.printStackTrace(new java.io.PrintWriter(out));
        String stackTrace = out.toString();
 
        return stackTrace;
    }
Did you like this? If so, please
tell a friend
about it, and subscribe to the blog RSS feed.

Share/Save/Bookmark

If you enjoyed this post, make sure you subscribe to my RSS feed!



Related Posts:
  • Recursively delete a directory
  • Sending an e-mail using WebSphere Mail session settings.
  • Sorting an ArrayList of objects.
  • LIKE clause with PreparedStatement
  • Defensive coping


  • No Responses to “Exception stackTrace to a String Variable”  

    1. No Comments

    Leave a Reply