Java Utililty Methods Stacktrace to String

List of utility methods to do Stacktrace to String

Description

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

Method

StringstackTraceToString(Exception exception)
Converts a stack trace of an exception to a string.
String stackTrace = "An exception occured:\n";
StackTraceElement trace[] = exception.getStackTrace();
for (StackTraceElement element : trace)
    stackTrace += "@ " + element.toString() + "\n";
return stackTrace;
StringstackTraceToString(final StackTraceElement[] stackTrace)
stack Trace To String
final StringBuilder sb = new StringBuilder();
for (final StackTraceElement stackTraceElement : stackTrace) {
    if (sb.length() > 0) {
        sb.append("\n");
    sb.append(stackTraceElement);
return sb.toString();
...
StringstackTraceToString(final Throwable t, final boolean header, final boolean trim)
Just return the stack trace as new-line-separated string.
final StringBuilder b = new StringBuilder(325);
if (header) {
    b.append(t.toString()); 
    b.append("\n");
final StackTraceElement[] elements = t.getStackTrace();
StackTraceElement last = null; 
int repetition = 0;
...
StringstacktraceToString(final Throwable throwable)
stacktrace To String
if (throwable == null)
    return DEFAULT;
String result = throwable.toString();
result += LINE_SEPARATOR;
int i = 0;
for (StackTraceElement element : throwable.getStackTrace()) {
    if (i > MAX_VISIBLE_ELEMENTS) {
        result += DOTS;
...
StringstacktraceToString(StackTraceElement[] s)
stacktrace To String
StringBuilder toReturn = new StringBuilder();
for (StackTraceElement e : s) {
    toReturn.append(e.getClassName() + " - " + e.getLineNumber() + " : " + e.getMethodName());
    toReturn.append(NEWLINE);
return toReturn.toString();
StringstackTraceToString(StackTraceElement[] stackTrace)
stack Trace To String
StringBuilder sb = new StringBuilder();
for (StackTraceElement element : stackTrace) {
    sb.append(element.toString()).append(" (").append(element.getLineNumber()).append(")");
    sb.append("\n");
return sb.toString();
StringstackTraceToString(StackTraceElement[] stackTrace)
stack Trace To String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < stackTrace.length; i++) {
    StackTraceElement el = stackTrace[i];
    sb.append("    ").append(el.toString()).append("\n");
return sb.toString();
voidstackTraceToString(StackTraceElement[] stackTrace, StringBuilder buf)
stack Trace To String
String sep = System.getProperty("line.separator");
for (int i = 0; i < stackTrace.length; i++) {
    StackTraceElement element = stackTrace[i];
    buf.append(sep + "\tat " + element.toString());
StringstackTraceToString(StackTraceElement[] ste)
stack Trace To String
String str = "";
for (int i = 0; i < ste.length; i++) {
    str += String.format("    [%02d] %s \n", i, ste[i].toString());
return str;
StringstackTraceToString(StackTraceElement[] trace)
Converts a StackTrace[] to a String, one item per line.
if (trace == null)
    return ("NULL stack trace.");
String s = "";
for (int i = 0; i < trace.length; i++) {
    if (trace[i] != null)
        s += trace[i].toString() + "\n";
    else
        s += "null trace element at [" + i + "]";
...