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

StringgetStackTraceAsString(Throwable t)
Gets the stack trace of the Throwable passed in as a string.
ByteArrayOutputStream stackTraceBIS = new ByteArrayOutputStream();
t.printStackTrace(new PrintStream(stackTraceBIS));
return new String(stackTraceBIS.toByteArray());
StringgetStackTraceAsString(Throwable t)
get Stack Trace As String
if (t != null) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    final String stackTraceAsString = sw.toString();
    try {
        sw.close();
    } catch (IOException e) {
...
StringgetStackTraceAsString(Throwable t)
Prints the Throwable object's stack trace into a string, so that it can be safely printed without the possibility of throwing an exception
final Writer sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString();
StringgetStackTraceAsString(Throwable t)
Given a Throwable, this method returns a String representation of the complete stack trace.
if (t != null) {
    StringWriter sw = new StringWriter();
    PrintWriter writer = new PrintWriter(sw);
    t.printStackTrace(writer);
    return sw.getBuffer().toString();
} else {
    return "";
StringgetStackTraceAsString(Throwable throwable)
get Stack Trace As String
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
StringgetStackTraceAsString(Throwable throwable)
Returns a string containing the result of Throwable#toString() toString() , followed by the full, recursive stack trace of throwable .
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
StringgetStackTraceAsText(Throwable t)
get Stack Trace As Text
ByteArrayOutputStream exceptionTraceText = new ByteArrayOutputStream();
PrintStream exceptionTrace = new PrintStream(exceptionTraceText);
t.printStackTrace(exceptionTrace);
return exceptionTraceText.toString();
StringgetStackTraceFromBaseException(Throwable exc)
get Stack Trace From Base Exception
String str = "";
try {
    Exception be = (Exception) exc;
    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, false, "UTF8");
    be.printStackTrace(ps);
    str = baos.toString("UTF8");
} catch (Exception e) {
...
StringgetStackTraceFromThrowable(Throwable t)
get Stack Trace From Throwable
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.println();
if (t.getCause() != null) {
    t.getCause().printStackTrace(pw);
return sw.toString();
...
StringgetStackTraceInString(Throwable throwable)
get Stack Trace In String
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteArrayOutputStream);
throwable.printStackTrace(printStream);
printStream.flush();
return byteArrayOutputStream.toString();