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(Throwable t)
get Stack Trace
return printStackTrace(t);
StringgetStackTrace(Throwable t)
Get an error's stack trace as a string.
String result = null;
try {
    final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    final PrintStream out = new PrintStream(bytesOut);
    t.printStackTrace(out);
    out.close();
    bytesOut.close();
    result = new String(bytesOut.toString("UTF-8"));
...
StringgetStackTrace(Throwable t)
This method returns the stack trace of a Throwable instance as a String.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
StringgetStackTrace(Throwable t)
Get StackTrace String from Throwable
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
StringgetStackTrace(Throwable t)
Return a String version of a stack trace
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();
stackTrace = stackTrace.replaceAll("&lt", "<");
stackTrace = stackTrace.replaceAll("&gt", ">");
return stackTrace;
StringGetStacktrace(Throwable t)
Get Stacktrace
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
boolean first = true;
Throwable tt = t;
do {
    if (!first) {
        printWriter.append("Caused by: ");
    first = false;
    tt.printStackTrace(printWriter);
} while ((tt = tt.getCause()) != null);
return result.toString();
StringgetStackTrace(Throwable t)
get Stack Trace
if (t == null) {
    return "THROWABLE-WAS-NULL (at " + getStackTrace(new Exception()) + ")";
try {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    Throwable cause = t.getCause();
...
StringgetStackTrace(Throwable t)
Get a stack trace as a string.
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
t.printStackTrace(printWriter);
return result.toString();
StringgetStackTrace(Throwable t)
Gets the stack trace of the given Throwable as a String.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
StringgetStackTrace(Throwable t)
A convenient way of extracting the stack trace from an exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
return sw.getBuffer().toString();