Here you can find the source of stackTraceToString(Throwable e)
public static String stackTraceToString(Throwable e)
//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(); } }