Java Throwable to String getStackTrace(final Throwable t)

Here you can find the source of getStackTrace(final Throwable t)

Description

Returns the stack trace of the given exception as a string.

License

Open Source License

Parameter

Parameter Description
t The exception whose stack trace is to be unwrapped and returned as a string.

Return

A string representation of the exception's stack trace

Declaration

public static String getStackTrace(final Throwable t) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**/*from  ww w .j a v  a  2  s . c  o m*/
     * <p>
     * Returns the stack trace of the given exception as a string.
     * </p>
     * 
     * @param t
     *            The exception whose stack trace is to be unwrapped and
     *            returned as a string.
     * @return A string representation of the exception's stack trace
     */
    public static String getStackTrace(final Throwable t) {
        StringBuffer result = new StringBuffer();

        try {
            StringWriter writeBuffer = new StringWriter();
            PrintWriter printBuffer = new PrintWriter(writeBuffer);

            // write the stack buffer into the StringWriter
            t.printStackTrace(printBuffer);

            printBuffer.flush();
            printBuffer.close();

            result.append(writeBuffer.toString());
        } catch (Throwable t2) {
            result.append("Unexpected error getting stack trace: " + t2.getMessage());
        }
        return result.toString();
    }
}

Related

  1. getStackTrace(@Nonnull final Throwable throwable)
  2. getStackTrace(final Throwable aThrowable)
  3. getStackTrace(final Throwable e)
  4. getStackTrace(final Throwable error)
  5. getStackTrace(final Throwable exception)
  6. getStackTrace(final Throwable t)
  7. getStackTrace(final Throwable t)
  8. getStackTrace(final Throwable t)
  9. getStackTrace(final Throwable throwable)