Java Stacktrace to String stackTraceToString(Throwable e)

Here you can find the source of stackTraceToString(Throwable e)

Description

stack Trace To String

License

Apache License

Declaration

public static String stackTraceToString(Throwable e) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int MAX_STACKTRACE_CAUSE_DEPTH = 5;

    public static String stackTraceToString(Throwable e) {
        return stackTraceToString(e, 0);
    }/*w  w w  .  j a  va2s . com*/

    public static String stackTraceToString(Throwable e, int depth) {
        StringBuilder sb = new StringBuilder();
        for (StackTraceElement element : e.getStackTrace()) {
            sb.append(element.toString());
            sb.append("\n");
        }
        if (depth < MAX_STACKTRACE_CAUSE_DEPTH && e.getCause() != null) {
            // While there is an underlying cause below the max depth, append it
            return sb.toString() + stackTraceToString(e.getCause(), ++depth);
        }
        return sb.toString();
    }
}

Related

  1. stackTraceToString(StackTraceElement[] stackTrace, StringBuilder buf)
  2. stackTraceToString(StackTraceElement[] ste)
  3. stackTraceToString(StackTraceElement[] trace)
  4. stackTraceToString(StackTraceElement[] trace)
  5. stackTraceToString(Throwable e)
  6. stackTraceToString(Throwable e)
  7. stackTraceToString(Throwable throwable)