Java Utililty Methods Throwable to String

List of utility methods to do Throwable to String

Description

The list of methods to do Throwable to String are organized into topic(s).

Method

StringgetStackTrace(final Throwable exception)
Returns the exception trace in the form of string.
final StringWriter strWriter = new StringWriter();
if (exception.getCause() == null) {
    exception.printStackTrace(new PrintWriter(strWriter, true));
} else {
    exception.getCause().printStackTrace(new PrintWriter(strWriter, true));
strWriter.flush();
return strWriter.toString();
...
StringgetStackTrace(final Throwable t)

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

StringBuffer result = new StringBuffer();
try {
    StringWriter writeBuffer = new StringWriter();
    PrintWriter printBuffer = new PrintWriter(writeBuffer);
    t.printStackTrace(printBuffer);
    printBuffer.flush();
    printBuffer.close();
    result.append(writeBuffer.toString());
...
StringgetStackTrace(final Throwable t)
get Stack Trace
final ByteArrayOutputStream bas = new ByteArrayOutputStream();
final PrintWriter pw = new PrintWriter(bas);
t.printStackTrace(pw);
pw.close();
return bas.toString();
StringgetStackTrace(final Throwable t)
Extracts the given exception's corresponding stack trace to a string.
try {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    t.printStackTrace(new PrintStream(out, false, "UTF-8"));
    return new String(out.toByteArray(), "UTF-8");
} catch (final IOException exc) {
    return null;
StringgetStackTrace(final Throwable t)
Get a stack trace as a string.
final StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString();
StringgetStackTrace(final Throwable throwable)

Gets the stack trace from a Throwable as a String.

The result of this method vary by JDK version as this method uses Throwable#printStackTrace(PrintWriter) .

final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
StringgetStackTrace(Throwable aThrowable)
Gets the stack trace.
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
StringgetStackTrace(Throwable aThrowable)
getStackTrace:(Describe the usage of this method).
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
StringgetStackTrace(Throwable e)
get Stack Trace
StringWriter out = new StringWriter();
PrintWriter w = new PrintWriter(out);
e.printStackTrace(w);
w.flush();
return out.toString();
StringgetStackTrace(Throwable e)
get exception stack trace
String stackTrace = "";
Writer writer = null;
PrintWriter printWriter = null;
try {
    writer = new StringWriter();
    printWriter = new PrintWriter(writer);
    e.printStackTrace(printWriter);
    stackTrace = writer.toString();
...