Sunday, October 30, 2011

How to get the Exception StackTrace content in java

How to get the error/exception Stacktrace in Java


/**
 * util method to get the full stacktrace of an exception
 * and return the message as string
 * @param e
 * @return
 */
public String getExceptionStack(Throwable e) {
 String errorMessage ="" ;
 try {
       StringWriter traceWriter = new StringWriter();
       PrintWriter printWriter = new PrintWriter(traceWriter, false);
       e.printStackTrace(printWriter);
       printWriter.close();
       errorMessage = traceWriter.getBuffer().toString();
 } catch (Throwable e1) {
      logger.error(e1.getMessage());
 }
   return errorMessage;
}




No comments:

Post a Comment