Java Stacktrace to String stackTraceToString(Exception e, int nBack)

Here you can find the source of stackTraceToString(Exception e, int nBack)

Description

stack Trace To String

License

Open Source License

Declaration

public static String stackTraceToString(Exception e, int nBack) 

Method Source Code

//package com.java2s;

public class Main {
    public static String stackTraceToString(Exception e, int nBack) {
        return stackTraceToString(e.getStackTrace(), nBack);
    }// w  ww.j av  a2  s.  c  om

    public static String stackTraceToString(StackTraceElement[] trace, int nBack) {
        int index = 0;
        if (trace.length < (index + 1)) {
            return "n/a";
        }

        StringBuilder sb = new StringBuilder("trace...");
        while (index < trace.length) {
            StackTraceElement elem = trace[index];
            sb.append(String.format("%n%s.%s line %s", elem.getClassName(), elem.getMethodName(),
                    elem.getLineNumber()));
            nBack--;
            if (nBack <= 0) {
                break;
            }

            index++;
        }

        return sb.toString();
    }
}

Related

  1. stackTraceAsString(Exception ex)
  2. stackTraceContains(String... needles)
  3. stackTraceStr(Exception e)
  4. stackTraceString(Throwable t)
  5. stackTraceToString(Exception e)
  6. stackTraceToString(Exception ex, String message)
  7. stackTraceToString(Exception exception)
  8. stackTraceToString(final StackTraceElement[] stackTrace)
  9. stackTraceToString(final Throwable t, final boolean header, final boolean trim)