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.flexive.tests.browser.AbstractSeleniumTest.java

/**
 * Clicks multiple times (in case of timeout) on a link
 * <p/>/*from   ww w .j a  v a2 s  .  c  o m*/
 * to have always the right stacktrace
 *
 * @param clickTarget
 * @param ms
 */
private void clickAndWait_(String clickTarget, int ms) {
    int trys = 20;
    ms >>= 3;
    boolean error = false;
    String code = "";
    int errLeft = 5;
    while (trys-- > 0) {
        try {
            selenium.click(clickTarget);
            selenium.waitForPageToLoad("" + ms);
            if (error) {
                System.err.println(
                        "you ment \"" + clickTarget + "\", but I corrected it for you! [" + code + "]");
            }
            break;
        } catch (Throwable t) {
            if (t.getMessage().endsWith(" not found") && errLeft-- <= 0 && !error) {
                clickTarget = "link=" + clickTarget;
                error = true;
                code = "" + t.getStackTrace()[4];
            }
            sleep(200);
        }
    }
}

From source file:com.fujitsu.dc.core.DcCoreAuthnException.java

/**
 * ?????????./*  w  w w.j  a va 2s  .  c o  m*/
 * @param t 
 * @return DcCoreException
 */
public DcCoreException reason(final Throwable t) {
    // ???
    DcCoreException ret = new DcCoreAuthnException(this.code, this.severity, this.message, this.status,
            this.error, this.realm);
    // 
    ret.setStackTrace(t.getStackTrace());
    return ret;
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBConnector.java

public void logConnectionStatus(boolean opening) {
    Throwable t = new Throwable();
    StackTraceElement[] elements = t.getStackTrace();

    StringBuffer stack = new StringBuffer();
    for (int i = 3; i > 0; i--) {
        String callerMethodName = elements[i].getMethodName();
        String callerClassName = elements[i].getClassName();
        stack.append(i + ": CallerClassName=" + callerClassName + " , Caller method name: " + callerMethodName
                + " -->");
    }/*from ww w.  j a  va 2s. c  om*/

    log.info(stack.toString());

    if (opening) {
        log.info("Opening connection. Number of Connections: " + openConnCount);
    } else {
        log.info("Closing connection. Number of Connections left: " + openConnCount);
    }
}

From source file:org.openadaptor.auxil.convertor.exception.ExceptionToOrderedMapConvertor.java

/**
 * Converts stack trace of an exception (and its root cause) to a String.
 * @param exception the processed exception.
 * @return stack trace as a String./*from  w ww  .java  2  s .co m*/
 */
private String getStackTraceAsString(Exception exception) {
    StringBuffer stackTraceBuf = new StringBuffer();
    StackTraceElement[] stackTrace = exception.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        stackTraceBuf.append(stackTrace[i]);
        stackTraceBuf.append("\n");
    }
    /* Append cause exception stack trace */
    Throwable cause = exception.getCause();
    if (cause != null) {
        stackTraceBuf.append("\n\n");
        stackTrace = cause.getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            stackTraceBuf.append(stackTrace[i]);
            stackTraceBuf.append("\n");
        }
    }
    return stackTraceBuf.toString();
}

From source file:com.asociate.managedbean.AsociateError.java

/**
 *
 * @return/*  w  w  w  . j ava  2  s .  co  m*/
 */
public String putStackTrace() {

    FacesContext context = FacesContext.getCurrentInstance();

    Map<String, Object> map = context.getExternalContext().getRequestMap();

    Throwable throwable = (Throwable) map.get("javax.servlet.error.exception");

    StringBuilder builder = new StringBuilder();

    if (throwable != null && throwable.getMessage() != null && !throwable.getMessage().isEmpty()) {

        builder.append(throwable.getMessage()).append("\n");

        for (StackTraceElement element : throwable.getStackTrace()) {

            builder.append(element).append("\n");

        }

        logger.debug("Ocurrio un error: " + builder.toString());
        return "Ha ocurrido un error...";

    }

    return "";

}

From source file:org.wso2.carbon.logging.service.provider.InMemoryLogProvider.java

private String getStacktrace(Throwable e) {
    StringBuilder stackTrace = new StringBuilder();
    StackTraceElement[] stackTraceElements = e.getStackTrace();
    for (StackTraceElement ele : stackTraceElements) {
        stackTrace.append(ele.toString()).append("\n");
    }/*ww  w. j  a  va 2  s. co m*/
    return stackTrace.toString();
}

From source file:org.jtrfp.trcl.flow.GameShell.java

private void throwable2StringBuilder(Throwable e, StringBuilder sb) {
    assert e != null;
    assert sb != null;
    sb.append(e.getClass().getName() + " " + e.getLocalizedMessage() + "\n");
    final StackTraceElement[] stackTraceElements = e.getStackTrace();
    for (StackTraceElement ste : stackTraceElements)
        sb.append("\tat " + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName() + ":"
                + ste.getLineNumber() + ")\n");
}

From source file:com.boundlessgeo.geoserver.api.controllers.IO.java

/** Encode an exception */
public static JSONObj error(JSONObj json, Throwable error) {
    if (error != null) {
        String message = null;//  www  . j a  va2s . c  om
        JSONArr cause = new JSONArr();
        for (Throwable t : Throwables.getCausalChain(error)) {
            if (message == null && t.getMessage() != null) {
                message = t.getMessage();
            }
            StringBuilder trace = new StringBuilder();
            for (StackTraceElement e : t.getStackTrace()) {
                trace.append(e.toString()).append('\n');
            }
            cause.addObject().put("exception", t.getClass().getSimpleName()).put("message", t.getMessage())
                    .put("trace", trace.toString());
        }
        if (message == null) {
            message = error.getClass().getSimpleName();
        }
        json.put("message", message != null ? message : error.toString()).put("cause", cause).put("trace",
                Throwables.getStackTraceAsString(error));
    }
    return json;
}

From source file:org.apache.log4j.layout.Log4j1XmlLayout.java

private void formatTo(final LogEvent event, final StringBuilder buf) {
    // We yield to the \r\n heresy.

    buf.append("<log4j:event logger=\"");
    buf.append(Transform.escapeHtmlTags(event.getLoggerName()));
    buf.append("\" timestamp=\"");
    buf.append(event.getTimeMillis());// w w  w  .j  a  v a2 s .  c o m
    buf.append("\" level=\"");
    buf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel())));
    buf.append("\" thread=\"");
    buf.append(Transform.escapeHtmlTags(event.getThreadName()));
    buf.append("\">\r\n");

    buf.append("<log4j:message><![CDATA[");
    // Append the rendered message. Also make sure to escape any existing CDATA sections.
    Transform.appendEscapingCData(buf, event.getMessage().getFormattedMessage());
    buf.append("]]></log4j:message>\r\n");

    List<String> ndc = event.getContextStack().asList();
    if (!ndc.isEmpty()) {
        buf.append("<log4j:NDC><![CDATA[");
        Transform.appendEscapingCData(buf, StringUtils.join(ndc, ' '));
        buf.append("]]></log4j:NDC>\r\n");
    }

    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    Throwable thrown = event.getThrown();
    if (thrown != null) {
        buf.append("<log4j:throwable><![CDATA[");
        buf.append(thrown.toString());
        buf.append("\r\n");
        for (StackTraceElement element : thrown.getStackTrace()) {
            Transform.appendEscapingCData(buf, "\tat " + element.toString());
            buf.append("\r\n");
        }
        buf.append("]]></log4j:throwable>\r\n");
    }

    if (locationInfo) {
        StackTraceElement source = event.getSource();
        if (source != null) {
            buf.append("<log4j:locationInfo class=\"");
            buf.append(Transform.escapeHtmlTags(source.getClassName()));
            buf.append("\" method=\"");
            buf.append(Transform.escapeHtmlTags(source.getMethodName()));
            buf.append("\" file=\"");
            buf.append(Transform.escapeHtmlTags(source.getFileName()));
            buf.append("\" line=\"");
            buf.append(source.getLineNumber());
            buf.append("\"/>\r\n");
        }
    }

    if (properties) {
        Map<String, String> contextMap = event.getContextMap();
        if (!contextMap.isEmpty()) {
            buf.append("<log4j:properties>\r\n");
            Object[] keys = contextMap.keySet().toArray();
            Arrays.sort(keys);
            for (Object key1 : keys) {
                String key = key1.toString();
                String val = contextMap.get(key);
                if (val != null) {
                    buf.append("<log4j:data name=\"");
                    buf.append(Transform.escapeHtmlTags(key));
                    buf.append("\" value=\"");
                    buf.append(Transform.escapeHtmlTags(val));
                    buf.append("\"/>\r\n");
                }
            }
            buf.append("</log4j:properties>\r\n");
        }
    }

    buf.append("</log4j:event>\r\n\r\n");
}

From source file:org.opentraces.metatracker.net.OpenTracesClient.java

public void uploadFile(String fileName) {
    try {/*from   www . j a  v  a  2  s. com*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        File f = new File(fileName);

        HttpPost httpost = new HttpPost("http://local.geotracing.com/tland/media.srv");
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("myIdentifier", new StringBody("somevalue"));
        entity.addPart("myFile", new FileBody(f));
        httpost.setEntity(entity);

        HttpResponse response;

        response = httpclient.execute(httpost);

        Log.d(LOG_TAG, "Upload result: " + response.getStatusLine());

        if (entity != null) {
            entity.consumeContent();
        }

        httpclient.getConnectionManager().shutdown();

    } catch (Throwable ex) {
        Log.d(LOG_TAG, "Upload failed: " + ex.getMessage() + " Stacktrace: " + ex.getStackTrace());
    }

}