Wednesday, February 16, 2011

Stack trace as String

Converting a stack trace into a String is done with Throwable.printStackTrace(PrintWriter).

If desired, a customized stack trace String can be defined by using the StackTraceElement class, as shown below.

Example

import java.io.*;
/**
* Simple utilities to return the stack trace of an
* exception as a String.
*/
public final class StackTraceUtil {
  public static String getStackTrace(Throwable aThrowable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    aThrowable.printStackTrace(printWriter);
    return result.toString();
  }
  /**
  * Defines a custom format for the stack trace as String.
  */
  public static String getCustomStackTrace(Throwable aThrowable) {
    //add the class name and any message passed to constructor
    final StringBuilder result = new StringBuilder( "BOO-BOO: " );
    result.append(aThrowable.toString());
    final String NEW_LINE = System.getProperty("line.separator");
    result.append(NEW_LINE);
    //add each element of the stack trace
    for (StackTraceElement element : aThrowable.getStackTrace() ){
      result.append( element );
      result.append( NEW_LINE );
    }
    return result.toString();
  }
  /** Demonstrate output.  */
  public static void main (String... aArguments){
    final Throwable throwable = new IllegalArgumentException("Blah");
    System.out.println( getStackTrace(throwable) );
    System.out.println( getCustomStackTrace(throwable) );
  }
} 
An example run of this class : 

>java -cp . StackTraceUtil
java.lang.IllegalArgumentException: Blah
        at StackTraceUtil.main(StackTraceUtil.java:42)

BOO-BOO: java.lang.IllegalArgumentException: Blah
StackTraceUtil.main(StackTraceUtil.java:42)

 
 

No comments:

Post a Comment

Chitika