Example usage for java.lang Throwable getStackTrace

List of usage examples for java.lang Throwable getStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:org.gc.networktester.util.Util.java

/** Go up the chain of exception causes, and print a nice trace of that all. */
public static String printException(Exception e) {
    StringBuilder out = new StringBuilder();
    String causePrefix = "";
    Throwable t = e;
    while (t != null) {
        out.append(causePrefix).append("exception: ").append(t).append(" at: ");
        if (t.getCause() == null) {
            // no more cause, print full backtrace now because that's the most interesting exception
            out.append("\n").append(backtraceFull(t.getStackTrace(), 0)).append("\n");
        } else {//w w w  .j av a 2 s.c  om
            // there's a cause, print only one line of trace because that is not the most interesting exception
            out.append(t.getStackTrace()[0]).append("\n");
        }
        t = t.getCause();
        // grow the cause prefix 
        causePrefix += "...cause: ";
    }
    return out.toString();
}

From source file:com.ichi2.anki.AnkiDroidApp.java

private static String getExceptionHash(Throwable th) {
    final StringBuilder res = new StringBuilder();
    Throwable cause = th;
    while (cause != null) {
        final StackTraceElement[] stackTraceElements = cause.getStackTrace();
        for (final StackTraceElement e : stackTraceElements) {
            res.append(e.getClassName());
            res.append(e.getMethodName());
        }/*from   w  ww.j  a  v  a 2s.c om*/
        cause = cause.getCause();
    }
    return Integer.toHexString(res.toString().hashCode());
}

From source file:com.notonthehighstreet.ratel.internal.model.Error.java

private static List<Backtrace> unwrapStackTrace(final Throwable e) {
    // As Honeybadger only supports a single error, we need to massage the wrapped exceptions into a single stack trace
    final List<Backtrace> backtraces = new ArrayList<Backtrace>();

    Throwable cause = e;//from   w ww  .ja  va  2  s . c  om

    while (cause != null) {
        if (!backtraces.isEmpty()) {
            backtraces.add(Backtrace.markerBacktrace(cause));
        }

        for (final StackTraceElement element : e.getStackTrace()) {
            backtraces.add(Backtrace.fromStackTrace(element));
        }

        cause = cause.getCause();
    }

    return backtraces;
}

From source file:org.jretty.log.Jdk14Logger.java

private static String getMethodInfo(String loggerName) {
    // Hack to get the stack trace.
    Throwable dummyException = new Throwable();
    StackTraceElement st[] = dummyException.getStackTrace();

    if (st.length > pos) {
        if (st[pos].getClassName().equals(loggerName)) {
            return "#" + st[pos].getMethodName() + "-" + st[pos].getLineNumber();
        }// w  ww  . j  a v  a2 s .c o  m
        return st[pos].getClassName() + "#" + st[pos].getMethodName() + "-" + st[pos].getLineNumber();
    }
    return "unknown";
}

From source file:griffon.util.GriffonUtil.java

/**
 * <p>Remove all apparently Griffon-internal trace entries from the exception instance<p>
 * <p>This modifies the original instance and returns it, it does not clone</p>
 * @param t//from  w  ww . jav  a2  s  .c  om
 * @return The exception passed in, after cleaning the stack trace
 */
public static Throwable sanitize(Throwable t) {
    // Note that this getProperty access may well be synced...
    if (!Boolean.valueOf(System.getProperty("griffon.full.stacktrace")).booleanValue()) {
        StackTraceElement[] trace = t.getStackTrace();
        List<StackTraceElement> newTrace = new ArrayList<StackTraceElement>();
        for (int i = 0; i < trace.length; i++) {
            StackTraceElement stackTraceElement = trace[i];
            if (isApplicationClass(stackTraceElement.getClassName())) {
                newTrace.add(stackTraceElement);
            }
        }

        // Only trim the trace if there was some application trace on the stack
        // if not we will just skip sanitizing and leave it as is
        if (newTrace.size() > 0) {
            // We don't want to lose anything, so log it
            STACK_LOG.error("Sanitizing stacktrace:", t);
            StackTraceElement[] clean = new StackTraceElement[newTrace.size()];
            newTrace.toArray(clean);
            t.setStackTrace(clean);
        }
    }
    return t;
}

From source file:org.evosuite.regression.RegressionAssertionCounter.java

private static String getSourceClassName(Throwable exception) {
    if (exception.getStackTrace().length == 0) {
        return null;
    }/* www  .j  a v a  2  s  . c  o  m*/
    return exception.getStackTrace()[0].getClassName();
}

From source file:org.openmrs.module.errorlogging.util.ExceptionLogUtil.java

/**
 * Gets information about exception and fills {@link ExceptionLog}  and {@link ExceptionLogDetail}.
 * If exception has a root cause fills {@link ExceptionLogRootCause} and {@link ExceptionLogRootCauseDetail}.
 * //from   w  ww .j a v a 2 s .  c  om
 * @param exception exception for parsing
 * @return exception log
 */
public static ExceptionLog parseException(Exception exception) {
    if (exception == null) {
        return null;
    }
    ExceptionLog excLog = new ExceptionLog(exception.getClass().getName(), exception.getMessage(),
            OpenmrsConstants.OPENMRS_VERSION_SHORT);
    boolean isJspException = false;
    if (exception.getClass().getName().equals("javax.servlet.jsp.JspException")
            || exception.getClass().getName().equals("org.apache.jasper.JasperException")) {
        isJspException = parseJspExceptionMessage(excLog, exception.getMessage());
    }
    if (!isJspException) {
        StackTraceElement[] stTrElements = exception.getStackTrace();
        if (stTrElements != null && stTrElements.length > 0) {
            ExceptionLogDetail excLogDetail = new ExceptionLogDetail(stTrElements[0].getFileName(),
                    stTrElements[0].getClassName(), stTrElements[0].getMethodName(),
                    stTrElements[0].getLineNumber());
            excLog.setExceptionLogDetail(excLogDetail);
        }
    }

    Throwable rootCause = ExceptionUtils.getRootCause(exception);
    if (rootCause != null) {
        ExceptionRootCause excRootCause = new ExceptionRootCause(rootCause.getClass().getName(),
                rootCause.getMessage());
        excLog.setExceptionRootCause(excRootCause);

        StackTraceElement[] stTrRootCauseElements = rootCause.getStackTrace();
        if (stTrRootCauseElements != null && stTrRootCauseElements.length > 0) {
            ExceptionRootCauseDetail excRootCauseDetail = new ExceptionRootCauseDetail(
                    stTrRootCauseElements[0].getFileName(), stTrRootCauseElements[0].getClassName(),
                    stTrRootCauseElements[0].getMethodName(), stTrRootCauseElements[0].getLineNumber());
            excRootCause.setExceptionRootCauseDetail(excRootCauseDetail);
        }
    }
    return excLog;
}

From source file:com.redhat.rhn.testing.TestUtils.java

/**
 * method to find a file relative to the calling class.  primarily
 * useful when writing tests that need access to external data.
 * this lets you put the data relative to the test class file.
 *
 * @param path the path, relative to caller's location
 * @return URL a URL referencing the file
 * @throws ClassNotFoundException if the calling class can not be found
 * (i.e., should not happen)/*  ww  w.  j a v a 2 s  .  c o m*/
 * @throws IOException if the specified file in an archive (eg. jar) and
 * it cannot be copied to a temporary location
 */
public static URL findTestData(String path) throws ClassNotFoundException, IOException {
    Throwable t = new Throwable();
    StackTraceElement[] ste = t.getStackTrace();

    String className = ste[1].getClassName();
    Class clazz = Class.forName(className);

    URL ret = clazz.getResource(path);

    if (ret.toString().contains("!")) { // file is from an archive
        String tempPath = "/tmp/" + filePrefix + ret.hashCode();
        InputStream input = clazz.getResourceAsStream(path);

        OutputStream output = new FileOutputStream(tempPath);
        IOUtils.copy(input, output);

        return new File(tempPath).toURI().toURL();
    }

    return ret;
}

From source file:org.jajuk.util.log.Log.java

/**
 * Spool an exception with stack traces.
 * //  www .  j a  va 2s  .  c o  m
 * @param e 
 */
private static void spool(Throwable e) {
    spool("<font color='red'>" + "[ERROR] " + e.getClass() + " / {{" + e.getMessage() + "}} / " + e.getCause());
    StackTraceElement[] ste = e.getStackTrace();
    for (StackTraceElement element : ste) {
        spool(element.toString());
    }
    spool(FONT_END);
}

From source file:de.alpharogroup.exception.ExceptionExtensions.java

/**
 * Gets the stack trace elements from the given Throwable and returns a {@link String} object
 * from it./* w  w w. j a va 2  s. com*/
 *
 * @param throwable
 *            the throwable
 * @return the stack trace elements
 */
public static String getStackTraceElements(Throwable throwable) {
    StringWriter sw = null;
    PrintWriter pw = null;
    String stacktrace = "throwable is null...";
    if (throwable != null) {
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            pw.println(throwable.getClass().toString());
            while (throwable != null) {
                pw.println(throwable);
                final StackTraceElement[] stackTraceElements = throwable.getStackTrace();
                for (final StackTraceElement stackTraceElement : stackTraceElements) {
                    pw.println("\tat " + stackTraceElement);
                }

                throwable = throwable.getCause();
                if (throwable != null) {
                    pw.println("Caused by:\r\n");
                }
            }
            stacktrace = sw.toString();
        } finally {
            StreamExtensions.closeWriter(sw);
            StreamExtensions.closeWriter(pw);
        }
    }

    return stacktrace;
}