Java Throwable to String getStackTraceAsString(Throwable t)

Here you can find the source of getStackTraceAsString(Throwable t)

Description

Given a Throwable, this method returns a String representation of the complete stack trace.

License

Open Source License

Parameter

Parameter Description
t the Throwable from which to extract the stack trace

Return

a String representation of the stack trace

Declaration

public static String getStackTraceAsString(Throwable t) 

Method Source Code


//package com.java2s;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**/*from   w  w w. j a va2  s. c o  m*/
     * Given a Throwable, this method returns a String representation of the
     * complete stack trace.
     *
     * @param t   the Throwable from which to extract the stack trace
     * @return  a String representation of the stack trace
     */
    public static String getStackTraceAsString(Throwable t) {

        if (t != null) {
            StringWriter sw = new StringWriter();
            PrintWriter writer = new PrintWriter(sw);
            t.printStackTrace(writer);
            return sw.getBuffer().toString();
        } else {
            return "";
        }
    }
}

Related

  1. getStackTraceAsString(Throwable ex)
  2. getStackTraceAsString(Throwable ex)
  3. getStackTraceAsString(Throwable t)
  4. getStackTraceAsString(Throwable t)
  5. getStackTraceAsString(Throwable t)
  6. getStackTraceAsString(Throwable throwable)
  7. getStackTraceAsString(Throwable throwable)
  8. getStackTraceAsText(Throwable t)
  9. getStackTraceFromBaseException(Throwable exc)