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

StringgetStackTraceLines(Throwable t, int numLines)
return the top n lines of this stack trace Example: ExceptionUtil.getStackTraceLines(exception, 10);
StringTokenizer tokens = new StringTokenizer(toString(t), "\n\r");
StringBuilder trace = new StringBuilder();
for (int i = 0; i < numLines; i++) {
    String token = tokens.nextToken();
    trace.append(token);
    trace.append("\n");
return trace.toString();
...
StringgetStackTraceOnly(Throwable throwable)
get Stack Trace Only
String stackTrace = getStackTrace(throwable);
if (stackTrace.indexOf("\tat") != -1) {
    return stackTrace.substring(stackTrace.indexOf("\tat"));
} else {
    return "";
StringgetStackTraceString(final Throwable currentEx)
Gets the stack trace of all the nested exceptions in the form of a string for the given exception.
String result = EMPTY_STRING;
if (currentEx != null) {
    final StringWriter stringWriter = new StringWriter();
    currentEx.printStackTrace(new PrintWriter(stringWriter));
    result = stringWriter.toString();
return result;
StringgetStackTraceString(Throwable cause, String delimiter, int maxLines)
get Stack Trace String
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
printWriter.print(cause.getMessage() + delimiter);
printWriter.print("(" + cause.getClass().getName() + ")" + delimiter);
printWriter.println();
StackTraceElement[] stackTrace = cause.getStackTrace();
for (int i = 0; i < stackTrace.length; i++) {
    printWriter.print(stackTrace[i] + delimiter);
...
StringgetStackTraceString(Throwable e)
get Stack Trace String
String str = null;
try {
    java.io.StringWriter sw = new java.io.StringWriter();
    e.printStackTrace(new java.io.PrintWriter(sw));
    str = sw.toString();
} catch (Throwable t) {
    str = e.toString();
return str;
ListgetStackTraceString(Throwable t)
get Stack Trace String
List lines = new ArrayList();
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    t.printStackTrace(ps);
    String stackTraces = baos.toString();
    StringTokenizer st = new StringTokenizer(stackTraces, "\n");
    while (st.hasMoreTokens()) {
...
StringgetStackTraceString(Throwable throwable)
Converts stack trace to string.
StringWriter sw = null;
PrintWriter pw = null;
try {
    sw = new StringWriter();
    pw = new PrintWriter(sw);
    pw.println(throwable.toString());
    throwable.printStackTrace(pw);
    pw.flush();
...
StringgetStackTraceString(Throwable throwable)
Converts stack trace to string.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println(throwable.toString());
throwable.printStackTrace(pw);
pw.flush();
return sw.toString();
StringgetStackTraceString(Throwable tr)
get Stack Trace String
if (tr == null) {
    return "";
Throwable t = tr;
while (t.getCause() != null) {
    t = t.getCause();
StringWriter sw = new StringWriter();
...
StringgetStackTraceWithoutCause(Throwable throwable)
get Stack Trace Without Cause
String result = "";
StackTraceElement[] trace = throwable.getStackTrace();
result += throwable.getLocalizedMessage();
for (StackTraceElement element : trace) {
    result += "\n at " + element;
return result + "\n";