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:com.machinepublishers.jbrowserdriver.diagnostics.Test.java

private static String testLabel(String result, int curTest, Throwable throwable) {
    String stackTrace = throwable == null ? "" : " -- " + toString(throwable);

    StackTraceElement[] elements = throwable == null ? new Throwable().getStackTrace()
            : throwable.getStackTrace();
    String testLocation = "";
    for (int i = 0; i < elements.length; i++) {
        if (Test.class.getName().equals(elements[i].getClassName())
                && "doTests".equals(elements[i].getMethodName())) {
            testLocation = elements[i].toString();
            break;
        }//from ww w .ja va  2  s.c o m
    }

    return "Test #" + curTest + " " + result + " -- " + testLocation + stackTrace;
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void fixRemoteStackTrace(Throwable remoteCause, StackTraceElement[] localSideStackTrace) {
    StackTraceElement[] remoteStackTrace = remoteCause.getStackTrace();
    StackTraceElement[] newStackTrace = new StackTraceElement[localSideStackTrace.length
            + remoteStackTrace.length];/*from   ww w.  j  ava 2  s.  c om*/
    System.arraycopy(remoteStackTrace, 0, newStackTrace, 0, remoteStackTrace.length);
    newStackTrace[remoteStackTrace.length] = new StackTraceElement(EXCEPTION_SEPARATOR, "", null, -1);
    System.arraycopy(localSideStackTrace, 1, newStackTrace, remoteStackTrace.length + 1,
            localSideStackTrace.length - 1);
    remoteCause.setStackTrace(newStackTrace);
}

From source file:org.nuxeo.ecm.webengine.servlet.WebServlet.java

protected static void displayError(HttpServletResponse resp, Throwable t, String message, int code) {
    PrintWriter writer;//from   ww w .j  ava  2  s.c  om
    try {
        writer = resp.getWriter();
    } catch (IOException e) {
        log.error("Unable to output Error ", e);
        log.error("Application error was " + message, e);
        return;
    }

    writer.write("\nError occured during Site rendering");
    writer.write("\nSite Error message : " + message);
    if (t != null) {
        writer.write("\nException message : " + t.getMessage());
        for (StackTraceElement element : t.getStackTrace()) {
            writer.write("\n" + element.toString());
        }
    }

    resp.setStatus(code);
}

From source file:org.sharegov.cirm.utils.GenUtils.java

public static void reportFatal(String subject, String msg, Throwable t) {
    ThreadLocalStopwatch.getWatch().time("ReportFatal email sent: " + msg + " " + t);
    logStackTrace(t.getStackTrace(), 10);

    OWLLiteral recipient = Refs.configSet.resolve().get("FatalErrorEmail");
    if (recipient == null)
        return;//  w  w  w  .  j a v  a2 s.c o m
    String body = "<p>Exception message:</p><p><b>" + msg + "</b></p>";
    body += "<p>" + new RestServiceAdmin().sysInfo().toString() + "</p>";
    if (t != null)
        body += "<hr><p>Stack Trace:</p>" + "<pre>" + stackTrace(t) + "</pre>";
    if (subject == null)
        subject = "";
    MessageManager.get().sendEmail("cirm@miamidade.gov", recipient.getLiteral(), "[CIRM FATAL] " + subject,
            body);
}

From source file:org.mule.config.ExceptionHelper.java

public static String getExceptionStack(Throwable t) {
    StringBuffer buf = new StringBuffer();
    // get exception stack
    List exceptions = getExceptionsAsList(t);

    int i = 1;//from  w w  w.ja  v  a 2  s . c o  m
    for (Iterator iterator = exceptions.iterator(); iterator.hasNext(); i++) {
        if (i > exceptionThreshold && exceptionThreshold > 0) {
            buf.append("(").append(exceptions.size() - i + 1).append(" more...)");
            break;
        }
        Throwable throwable = (Throwable) iterator.next();
        ExceptionReader er = getExceptionReader(throwable);
        buf.append(i).append(". ").append(er.getMessage(throwable)).append(" (");
        buf.append(throwable.getClass().getName()).append(")\n");
        if (verbose && throwable.getStackTrace().length > 0) {
            StackTraceElement e = throwable.getStackTrace()[0];
            buf.append("  ").append(e.getClassName()).append(":").append(e.getLineNumber()).append(" (")
                    .append(getJavaDocUrl(throwable.getClass())).append(")\n");
        }
    }
    return buf.toString();
}

From source file:edu.usu.sdl.openstorefront.common.util.StringProcessor.java

/**
 * This will produce html highlighted stacktrace
 *
 * @param throwable//  www.  j  a  va 2s . c  om
 * @return
 */
public static String parseStackTraceHtml(Throwable throwable) {
    StringBuilder exception = new StringBuilder();

    if (throwable != null) {
        String message = throwable.getMessage();
        if (StringUtils.isNotBlank(message)) {
            exception.append("<b>Message:</b> <span style='color: red;'><b>")
                    .append(message.replace("\n", "<br>")).append("</b><br>");
        }
        for (StackTraceElement stackTraceElement : throwable.getStackTrace()) {
            String style = "color: grey; font-size: 10px;";
            if (stackTraceElement.getClassName().contains("edu.usu.sdl")) {
                style = "color: black; font-size: 12px; font-wieght: bold;";
            }
            exception.append("<span style='").append(style).append("'>")
                    .append(stackTraceElement.getClassName()).append(" (")
                    .append(stackTraceElement.getMethodName()).append(") : ")
                    .append(stackTraceElement.getLineNumber()).append(" ").append("</span><br>");
        }
        if (throwable.getSuppressed().length > 0) {
            exception.append("Suppress Exceptions: ");
            for (Throwable suppressed : throwable.getSuppressed()) {
                exception.append(parseStackTraceHtml(suppressed)).append("<br>");
            }
        }

        if (throwable.getCause() != null) {

        }
    }

    return exception.toString();
}

From source file:sf.net.experimaestro.utils.JSUtils.java

/**
 * @see org.mozilla.javascript.RhinoException#getScriptStack()
 * @param ex/*from w  w w  .  ja  va  2  s . com*/
 * @return A stack
 */
static public ScriptStackElement[] getScriptStackTrace(Throwable ex) {
    List<ScriptStackElement> list = new ArrayList<>();
    ScriptStackElement[][] interpreterStack = null;
    int interpreterStackIndex = 0;
    StackTraceElement[] stack = ex.getStackTrace();
    // Pattern to recover function name from java method name -
    // see Codegen.getBodyMethodName()
    // kudos to Marc Guillemot for coming up with this
    Pattern pattern = Pattern.compile("_c_(.*)_\\d+");
    for (StackTraceElement e : stack) {
        String fileName = e.getFileName();
        if (e.getMethodName().startsWith("_c_") && e.getLineNumber() > -1 && fileName != null
                && !fileName.endsWith(".java")) {
            String methodName = e.getMethodName();
            Matcher match = pattern.matcher(methodName);
            // the method representing the main script is always "_c_script_0" -
            // at least we hope so
            methodName = !"_c_script_0".equals(methodName) && match.find() ? match.group(1) : null;
            list.add(new ScriptStackElement(fileName, methodName, e.getLineNumber()));
        } else if ("org.mozilla.javascript.Interpreter".equals(e.getClassName())
                && "interpretLoop".equals(e.getMethodName()) && interpreterStack != null
                && interpreterStack.length > interpreterStackIndex) {
            for (ScriptStackElement elem : interpreterStack[interpreterStackIndex++]) {
                list.add(elem);
            }
        }
    }
    return list.toArray(new ScriptStackElement[list.size()]);

}

From source file:Debug.java

public static String getStackTrace(Throwable e, int limit, int skip) {
    StringBuffer result = new StringBuffer();

    if (e != null) {
        StackTraceElement stes[] = e.getStackTrace();
        if (stes != null) {
            for (int i = skip; i < stes.length && (limit < 0 || i < limit); i++) {
                StackTraceElement ste = stes[i];

                result.append("\tat " + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName()
                        + ":" + ste.getLineNumber() + ")" + newline);
            }// w  w  w . j ava 2s  .  c o  m
            if (limit >= 0 && stes.length > limit)
                result.append("\t..." + newline);
        }

        //            e.printStackTrace(System.out);
        result.append(newline);
    }

    return result.toString();
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.model.Reports.java

public static Throwable newThrowable(java.lang.Throwable throwable) {
    Throwable mThrowable = factory.createThrowable();
    mThrowable.setMessage(throwable.getMessage());
    mThrowable.setClassName(throwable.getClass().getName());
    List<StackTraceElement> mStackTrace = mThrowable.getStackTrace();
    for (java.lang.StackTraceElement stackTraceElement : throwable.getStackTrace()) {
        StackTraceElement mStackTraceElement = factory.createStackTraceElement();
        mStackTraceElement.setFileName(stackTraceElement.getFileName());
        mStackTraceElement.setClassName(ensureNotBlank(stackTraceElement.getClassName(), throwable));
        mStackTraceElement.setMethodName(ensureNotBlank(stackTraceElement.getMethodName(), throwable));
        mStackTraceElement.setLineNumber(stackTraceElement.getLineNumber());
        mStackTraceElement.setNative(stackTraceElement.isNativeMethod());
        mStackTrace.add(mStackTraceElement);
    }/*from  ww w.j  av a 2s  .  com*/
    java.lang.Throwable cause = throwable.getCause();
    if (cause != null) {
        if (cause == throwable) {
            log(WARN_CYCLIC_EXCEPTION, cause.toString());
            return mThrowable;
        }
        mThrowable.setCause(newThrowable(cause));
    }
    return mThrowable;
}

From source file:com.codepunk.codepunklib.util.log.FormattingLogger.java

/**
 * Returns the first {@link StackTraceElement} in the stack trace found in <code>tr</code>
 * that was called from outside of this class.
 * @param tr The {@link Throwable} containing the stack trace.
 * @param index The number of levels in the stack trace that result from methods in this class.
 * @return The first significant {@link StackTraceElement}; that is, the first element
 * representing a call made outside of this class.
 *//*from   w  w  w .j  av a  2  s . c o m*/
private static StackTraceElement getSignificantStackTraceElement(Throwable tr, int index) {
    StackTraceElement element;
    try {
        if (tr == null) {
            element = (new Throwable()).getStackTrace()[index + 1];
        } else if (tr.getCause() == null) {
            element = tr.getStackTrace()[0];
        } else {
            element = tr.getCause().getStackTrace()[0];
        }
    } catch (Exception e) {
        element = null;
    }
    return element;
}