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.evosuite.regression.RegressionExceptionHelper.java

/**
 * This part is "temporarily" copied over from TestCodeVisitor.
 * Until they are made statically available to use in this class.
 *//*from ww w  .  j  ava2s .c  o  m*/
private static String getSourceClassName(Throwable exception) {
    if (exception.getStackTrace().length == 0) {
        return null;
    }
    return exception.getStackTrace()[0].getClassName();
}

From source file:com.cmsz.cloudplatform.utils.StringUtils.java

public static String getExceptionStackInfo(Throwable e) {
    StringBuffer sb = new StringBuffer();

    sb.append(e.toString()).append("\n");
    StackTraceElement[] elemnents = e.getStackTrace();
    for (StackTraceElement element : elemnents) {
        sb.append(element.getClassName()).append(".");
        sb.append(element.getMethodName()).append("(");
        sb.append(element.getFileName()).append(":");
        sb.append(element.getLineNumber()).append(")");
        sb.append("\n");
    }/* w ww.ja  v  a 2 s.  co  m*/

    return sb.toString();
}

From source file:com.infinities.skyport.timeout.ServiceProviderTimeLimiter.java

private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception {
    Throwable cause = e.getCause();
    if (cause == null) {
        throw e;/*from  w  ww  .j  a  va 2s  .  c om*/
    }
    if (combineStackTraces) {
        StackTraceElement[] combined = ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(),
                StackTraceElement.class);
        cause.setStackTrace(combined);
    }
    if (cause instanceof Exception) {
        throw (Exception) cause;
    }
    if (cause instanceof Error) {
        throw (Error) cause;
    }
    // The cause is a weird kind of Throwable, so throw the outer exception.
    throw e;
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static String getCurrentThreadStackTrace() {
    Throwable throwable = new Throwable();
    StackTraceElement[] ste = throwable.getStackTrace();
    StringBuffer sbuf = new StringBuffer();

    for (int i = ste.length - 1; i > 0; --i) {
        sbuf.append(ste[i].getClassName() + "." + ste[i].getMethodName());
        sbuf.append("/");
    }/*  w  w  w.  java  2s .co  m*/
    sbuf.deleteCharAt(sbuf.length() - 1);
    return sbuf.toString();
}

From source file:com.googlecode.flyway.commandline.Main.java

/**
 * Output class, method and line number infos of first stack trace element
 * of the given {@link Throwable} using {@link Log#error(Object)}.
 *
 * @param t {@link Throwable} to log/*from  w ww.j a  v  a2  s .c  o  m*/
 */
private static void outputFirstStackTraceElement(Throwable t) {
    StackTraceElement firstStackTraceElement = t.getStackTrace()[0];
    LOG.error("Occured in " + firstStackTraceElement.getClassName() + "."
            + firstStackTraceElement.getMethodName() + "() at line " + firstStackTraceElement.getLineNumber());
}

From source file:com.robwilliamson.healthyesther.Utils.java

public static String format(@Nonnull Throwable e) {
    StringBuilder str = new StringBuilder();
    final String nl = "/n";
    str.append(e.getMessage()).append(nl);
    str.append(e.getClass()).append(nl);
    for (StackTraceElement element : e.getStackTrace()) {
        str.append(element).append(nl);/*from   ww w .j  a v  a  2 s. c o  m*/
    }

    if (e.getCause() != null) {
        str.append("Caused by:").append(nl);
        str.append(format(e.getCause()));
    }

    return str.toString();
}

From source file:com.seleniumtests.reporter.reporters.CommonReporter.java

/**
 * Method to generate the formated stacktrace
 * @param exception      Exception to format
 * @param title         title of the exception
 * @param contentBuffer   /*from www.jav  a  2 s. com*/
 * @param format      format to use to encode ('html', 'csv', 'xml', 'json')
 */
public static void generateTheStackTrace(final Throwable exception, final String title,
        final StringBuilder contentBuffer, String format) {
    contentBuffer.append(exception.getClass() + ": " + StringUtility.encodeString(title, format) + "\n");

    StackTraceElement[] s1 = exception.getStackTrace();
    Throwable t2 = exception.getCause();
    if (t2 == exception) {
        t2 = null;
    }
    for (int x = 0; x < s1.length; x++) {
        String message = filterStackTrace(s1[x].toString());
        if (message != null) {
            contentBuffer.append("\nat " + StringUtility.encodeString(message, format));
        }
    }

    if (t2 != null) {
        generateTheStackTrace(t2, "Caused by " + t2.getLocalizedMessage(), contentBuffer, format);
    }
}

From source file:griffon.util.GriffonUtil.java

public static void printSanitizedStackTrace(Throwable t, PrintWriter p) {
    t = sanitize(t);//from  w  w w  .  j  av a  2  s.  c  o  m

    StackTraceElement[] trace = t.getStackTrace();
    for (int i = 0; i < trace.length; i++) {
        StackTraceElement stackTraceElement = trace[i];
        p.println("at " + stackTraceElement.getClassName() + "(" + stackTraceElement.getMethodName() + ":"
                + stackTraceElement.getLineNumber() + ")");
    }
}

From source file:com.codelanx.codelanxlib.util.exception.Exceptions.java

/**
 * Creates a readable stack trace from a passed {@link Throwable}. This
 * method will reproduce the same output that
 * {@link Throwable#printStackTrace()} would output
 *
 * @since 0.1.0/*  ww w.  ja  v  a2s .  c om*/
 * @version 0.1.0
 *
 * @param t The {@link Throwable} to make readable
 * @return A string representing the entire stack trace
 */
public static String readableStackTrace(Throwable t) {
    StringBuilder sb = new StringBuilder();
    StackTraceElement[] trace = t.getStackTrace();
    for (StackTraceElement elem : trace) {
        sb.append("\tat ").append(elem).append('\n');
    }
    if (t.getCause() != null) {
        Exceptions.readableStackTraceAsCause(sb, t.getCause(), trace);
    }
    return sb.toString();
}

From source file:com.bstek.dorado.view.resolver.PageOutputUtils.java

public static void outputException(Writer writer, Throwable throwable) throws IOException {
    writer.append("<h1 style=\"font-size:12pt; color:red\">");
    OutputUtils.outputString(writer,//w w w. j  av  a 2  s.c om
            StringUtils.defaultString(throwable.getMessage(), throwable.getClass().getName()));
    writer.append("</h1>\n");
    writer.append("<ul>\n");
    StackTraceElement[] stes = throwable.getStackTrace();
    for (int i = 0; i < stes.length; i++) {
        StackTraceElement ste = stes[i];
        writer.append("<li>").append("at ");
        OutputUtils.outputString(writer, ste.toString());
        writer.append("</li>\n");
    }
    writer.append("</ul>\n");
}