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.mili.core.logging.java.JavaAdapter.java

private void logException(Level level, Throwable t, boolean caused) {
    if (t == null) {
        return;/*from w  w  w  .j  av  a 2  s .c o m*/
    }
    StringBuilder s = new StringBuilder();
    if (caused) {
        s.append("caused by: ");
    }
    s.append(t.getClass().getName());
    s.append(": ");
    s.append(t.getMessage());
    log(s);
    s.setLength(0);
    List<StackTraceElement> l0 = Arrays.asList(t.getStackTrace());
    for (int i = 0, n = l0.size(); i < n; i++) {
        if (i > 0) {
            s.append("    ");
        }
        s.append(String.valueOf(l0.get(i)));
        log(s);
        s.setLength(0);
    }
    logException(level, t.getCause(), true);
    return;
}

From source file:org.mili.core.logging.log4j.Log4jAdapter.java

private void logException(Level level, Throwable t, boolean caused) {
    if (t == null) {
        return;/*from  ww w . j ava  2  s.c  o m*/
    }
    StringBuilder s = new StringBuilder();
    if (caused) {
        s.append("caused by: ");
    }
    s.append(t.getClass().getName());
    s.append(": ");
    s.append(t.getMessage());
    this.log(s);
    s.setLength(0);
    List<StackTraceElement> l0 = Arrays.asList(t.getStackTrace());
    for (int i = 0, n = l0.size(); i < n; i++) {
        if (i > 0) {
            s.append("    ");
        }
        s.append(String.valueOf(l0.get(i)));
        this.log(s);
        s.setLength(0);
    }
    this.logException(level, t.getCause(), true);
    return;
}

From source file:org.unitime.timetable.gwt.command.server.GwtRpcServlet.java

private <T extends GwtRpcResponse> void log(GwtRpcRequest<T> request, T response, Throwable exception,
        long time, SessionContext context, GwtRpcLogging logging) {
    try {//from   w w  w.  ja  va2  s. co m
        if (iSaver == null)
            return;
        if (logging != null) {
            switch (logging.value()) {
            case DISABLED:
                return;
            case ON_EXCEPTION:
                if (exception != null)
                    return;
            }
        }
        QueryLog q = new QueryLog();
        String requestName = request.getClass().getSimpleName();
        q.setUri("RPC:" + requestName);
        q.setType(QueryLog.Type.RPC.ordinal());
        q.setTimeStamp(new Date());
        q.setTimeSpent(time);
        q.setSessionId(context.getHttpSessionId());
        q.setUid(context.isAuthenticated() ? context.getUser().getTrueExternalUserId() : null);
        if (ApplicationProperty.QueryLogJSON.isTrue()) {
            q.setQuery(iGson.toJson(request));
        } else {
            q.setQuery(request.toString());
        }
        if (exception != null) {
            Throwable t = exception;
            String ex = "";
            while (t != null) {
                String clazz = t.getClass().getName();
                if (clazz.indexOf('.') >= 0)
                    clazz = clazz.substring(1 + clazz.lastIndexOf('.'));
                if (!ex.isEmpty())
                    ex += "\n";
                ex += clazz + ": " + t.getMessage();
                if (t.getStackTrace() != null && t.getStackTrace().length > 0)
                    ex += " (at " + t.getStackTrace()[0].getFileName() + ":"
                            + t.getStackTrace()[0].getLineNumber() + ")";
                t = t.getCause();
            }
            if (!ex.isEmpty())
                q.setException(ex);
        }
        iSaver.add(q);
    } catch (Throwable t) {
        sLog.warn("Failed to log a request: " + t.getMessage(), t);
    }
}

From source file:org.alfresco.extension.bulkfilesystemimport.webscripts.BulkFilesystemImportWebScript.java

private String renderExceptionStackAsText(final Throwable t) {
    StringBuffer result = new StringBuffer();

    if (t != null) {
        String message = t.getMessage();
        Throwable cause = t.getCause();

        if (cause != null) {
            result.append(renderExceptionStackAsText(cause));
            result.append("\nWrapped by:");
        }/*from www . j  ava 2s .  c om*/

        if (message == null) {
            message = "";
        }

        result.append("\n");
        result.append(t.getClass().getName());
        result.append(": ");
        result.append(message);
        result.append("\n");
        result.append(renderStackTraceElements(t.getStackTrace()));
    }

    return (result.toString());
}

From source file:edu.ku.brc.specify.tasks.subpane.LabelsPane.java

/**
 * @param throwable/* w  w w. j  a v a 2s .co m*/
 * @return
 */
protected String getStackTrace(Throwable throwable) {
    String sep = System.getProperty("line.separator");
    StringBuilder result = new StringBuilder();
    result.append(throwable.toString());
    result.append(sep);

    for (StackTraceElement element : throwable.getStackTrace()) {
        result.append(element);
        result.append(sep);
    }
    return result.toString();
}

From source file:io.teak.sdk.Raven.java

public void reportException(Throwable t) {
    if (t == null) {
        return;//from  w  ww .ja v  a 2 s.com
    }

    HashMap<String, Object> additions = new HashMap<>();
    ArrayList<Object> exceptions = new ArrayList<>();
    HashMap<String, Object> exception = new HashMap<>();

    exception.put("type", t.getClass().getSimpleName());
    exception.put("value", t.getMessage());
    exception.put("module", t.getClass().getPackage().getName());

    HashMap<String, Object> stacktrace = new HashMap<>();
    ArrayList<Object> stackFrames = new ArrayList<>();

    StackTraceElement[] steArray = t.getStackTrace();
    for (int i = steArray.length - 1; i >= 0; i--) {
        StackTraceElement ste = steArray[i];
        HashMap<String, Object> frame = new HashMap<>();

        frame.put("filename", ste.getFileName());

        String method = ste.getMethodName();
        if (method.length() != 0) {
            frame.put("function", method);
        }

        int lineno = ste.getLineNumber();
        if (!ste.isNativeMethod() && lineno >= 0) {
            frame.put("lineno", lineno);
        }

        String module = ste.getClassName();
        frame.put("module", module);

        boolean in_app = true;
        if (module.startsWith("android.") || module.startsWith("java.") || module.startsWith("dalvik.")
                || module.startsWith("com.android.")) {
            in_app = false;
        }

        frame.put("in_app", in_app);

        stackFrames.add(frame);
    }
    stacktrace.put("frames", stackFrames);

    exception.put("stacktrace", stacktrace);

    exceptions.add(exception);
    additions.put("exception", exceptions);

    try {
        Report report = new Report(t.getMessage(), Level.ERROR, additions);
        report.sendToService();
    } catch (Exception e) {
        Log.e(LOG_TAG, "Unable to report Teak SDK exception. " + Log.getStackTraceString(t) + "\n"
                + Log.getStackTraceString(e));
    }
}

From source file:org.apache.pig.backend.hadoop.executionengine.Launcher.java

/**
 *
 * @param stackTrace/*from w w  w  .jav  a  2s .com*/
 *            The string representation of
 *            {@link Throwable#printStackTrace() printStackTrace} Handles
 *            internal PigException and its subclasses that override the
 *            {@link Throwable#toString() toString} method
 * @return An exception object whose string representation of
 *         printStackTrace is the input stackTrace
 * @throws Exception
 */
public Exception getExceptionFromString(String stackTrace) throws Exception {
    String[] lines = stackTrace.split(newLine);
    Throwable t = getExceptionFromStrings(lines, 0);

    if (!pigException) {
        int errCode = 6015;
        String msg = "During execution, encountered a Hadoop error.";
        ExecException ee = new ExecException(msg, errCode, PigException.REMOTE_ENVIRONMENT, t);
        ee.setStackTrace(t.getStackTrace());
        return ee;
    } else {
        pigException = false;
        if (outOfMemory) {
            outOfMemory = false;
            int errCode = 6016;
            String msg = "Out of memory.";
            ExecException ee = new ExecException(msg, errCode, PigException.REMOTE_ENVIRONMENT, t);
            ee.setStackTrace(t.getStackTrace());
            return ee;
        }
        return (Exception) t;
    }
}

From source file:org.apache.archiva.web.test.parent.AbstractSeleniumTest.java

public String captureScreenShotOnFailure(Throwable failure, String methodName, String className) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH_mm_ss");
    String time = sdf.format(new Date());
    File targetPath = new File("target", "screenshots");

    int lineNumber = 0;

    for (StackTraceElement stackTrace : failure.getStackTrace()) {
        if (stackTrace.getClassName().equals(this.getClass().getName())) {
            lineNumber = stackTrace.getLineNumber();
            break;
        }//  w w w .ja v a 2  s  . c o  m
    }

    targetPath.mkdirs();
    Selenium selenium = getSelenium();
    String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;

    selenium.windowMaximize();

    try {
        // save html to have a minimum feedback if jenkins firefox not up
        File fileNameHTML = new File(new File("target", "errorshtmlsnap"), fileBaseName + ".html");
        FileUtils.writeStringToFile(fileNameHTML, selenium.getHtmlSource());
    } catch (IOException e) {
        System.out.print(e.getMessage());
        e.printStackTrace();
    }

    File fileName = new File(targetPath, fileBaseName + ".png");

    selenium.captureEntirePageScreenshot(fileName.getAbsolutePath(), "background=#FFFFFF");

    return fileName.getAbsolutePath();
}

From source file:hudson.FunctionsTest.java

@Issue("JDK-6507809")
@Test//  w  ww. ja va  2s  .  c  o  m
public void printThrowable() throws Exception {
    // Basics: a single exception. No change.
    assertPrintThrowable(new Stack("java.lang.NullPointerException: oops", "p.C.method1:17", "m.Main.main:1"),
            "java.lang.NullPointerException: oops\n" + "\tat p.C.method1(C.java:17)\n"
                    + "\tat m.Main.main(Main.java:1)\n",
            "java.lang.NullPointerException: oops\n" + "\tat p.C.method1(C.java:17)\n"
                    + "\tat m.Main.main(Main.java:1)\n");
    // try {} catch (Exception x) {throw new IllegalStateException(x);}
    assertPrintThrowable(
            new Stack("java.lang.IllegalStateException: java.lang.NullPointerException: oops", "p.C.method1:19",
                    "m.Main.main:1")
                            .cause(new Stack("java.lang.NullPointerException: oops", "p.C.method2:23",
                                    "p.C.method1:17", "m.Main.main:1")),
            "java.lang.IllegalStateException: java.lang.NullPointerException: oops\n"
                    + "\tat p.C.method1(C.java:19)\n" + "\tat m.Main.main(Main.java:1)\n"
                    + "Caused by: java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "\t... 1 more\n",
            "java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "Caused: java.lang.IllegalStateException\n"
                    + "\tat p.C.method1(C.java:19)\n" + "\tat m.Main.main(Main.java:1)\n");
    // try {} catch (Exception x) {throw new IllegalStateException("more info");}
    assertPrintThrowable(
            new Stack("java.lang.IllegalStateException: more info", "p.C.method1:19", "m.Main.main:1")
                    .cause(new Stack("java.lang.NullPointerException: oops", "p.C.method2:23", "p.C.method1:17",
                            "m.Main.main:1")),
            "java.lang.IllegalStateException: more info\n" + "\tat p.C.method1(C.java:19)\n"
                    + "\tat m.Main.main(Main.java:1)\n" + "Caused by: java.lang.NullPointerException: oops\n"
                    + "\tat p.C.method2(C.java:23)\n" + "\tat p.C.method1(C.java:17)\n" + "\t... 1 more\n",
            "java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "Caused: java.lang.IllegalStateException: more info\n"
                    + "\tat p.C.method1(C.java:19)\n" + "\tat m.Main.main(Main.java:1)\n");
    // try {} catch (Exception x) {throw new IllegalStateException("more info: " + x);}
    assertPrintThrowable(
            new Stack("java.lang.IllegalStateException: more info: java.lang.NullPointerException: oops",
                    "p.C.method1:19", "m.Main.main:1")
                            .cause(new Stack("java.lang.NullPointerException: oops", "p.C.method2:23",
                                    "p.C.method1:17", "m.Main.main:1")),
            "java.lang.IllegalStateException: more info: java.lang.NullPointerException: oops\n"
                    + "\tat p.C.method1(C.java:19)\n" + "\tat m.Main.main(Main.java:1)\n"
                    + "Caused by: java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "\t... 1 more\n",
            "java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "Caused: java.lang.IllegalStateException: more info\n"
                    + "\tat p.C.method1(C.java:19)\n" + "\tat m.Main.main(Main.java:1)\n");
    // Synthetic stack showing an exception made elsewhere, such as happens with hudson.remoting.Channel.attachCallSiteStackTrace.
    Throwable t = new Stack("remote.Exception: oops", "remote.Place.method:17", "remote.Service.run:9");
    StackTraceElement[] callSite = new Stack("wrapped.Exception", "local.Side.call:11", "local.Main.main:1")
            .getStackTrace();
    StackTraceElement[] original = t.getStackTrace();
    StackTraceElement[] combined = new StackTraceElement[original.length + 1 + callSite.length];
    System.arraycopy(original, 0, combined, 0, original.length);
    combined[original.length] = new StackTraceElement(".....", "remote call", null, -2);
    System.arraycopy(callSite, 0, combined, original.length + 1, callSite.length);
    t.setStackTrace(combined);
    assertPrintThrowable(t,
            "remote.Exception: oops\n" + "\tat remote.Place.method(Place.java:17)\n"
                    + "\tat remote.Service.run(Service.java:9)\n" + "\tat ......remote call(Native Method)\n"
                    + "\tat local.Side.call(Side.java:11)\n" + "\tat local.Main.main(Main.java:1)\n",
            "remote.Exception: oops\n" + "\tat remote.Place.method(Place.java:17)\n"
                    + "\tat remote.Service.run(Service.java:9)\n" + "\tat ......remote call(Native Method)\n"
                    + "\tat local.Side.call(Side.java:11)\n" + "\tat local.Main.main(Main.java:1)\n");
    // Same but now using a cause on the remote side.
    t = new Stack("remote.Wrapper: remote.Exception: oops", "remote.Place.method2:19", "remote.Service.run:9")
            .cause(new Stack("remote.Exception: oops", "remote.Place.method1:11", "remote.Place.method2:17",
                    "remote.Service.run:9"));
    callSite = new Stack("wrapped.Exception", "local.Side.call:11", "local.Main.main:1").getStackTrace();
    original = t.getStackTrace();
    combined = new StackTraceElement[original.length + 1 + callSite.length];
    System.arraycopy(original, 0, combined, 0, original.length);
    combined[original.length] = new StackTraceElement(".....", "remote call", null, -2);
    System.arraycopy(callSite, 0, combined, original.length + 1, callSite.length);
    t.setStackTrace(combined);
    assertPrintThrowable(t,
            "remote.Wrapper: remote.Exception: oops\n" + "\tat remote.Place.method2(Place.java:19)\n"
                    + "\tat remote.Service.run(Service.java:9)\n" + "\tat ......remote call(Native Method)\n"
                    + "\tat local.Side.call(Side.java:11)\n" + "\tat local.Main.main(Main.java:1)\n"
                    + "Caused by: remote.Exception: oops\n" + "\tat remote.Place.method1(Place.java:11)\n"
                    + "\tat remote.Place.method2(Place.java:17)\n"
                    + "\tat remote.Service.run(Service.java:9)\n",
            "remote.Exception: oops\n" + "\tat remote.Place.method1(Place.java:11)\n"
                    + "\tat remote.Place.method2(Place.java:17)\n" + "\tat remote.Service.run(Service.java:9)\n"
                    + // we do not know how to elide the common part in this case
                    "Caused: remote.Wrapper\n" + "\tat remote.Place.method2(Place.java:19)\n"
                    + "\tat remote.Service.run(Service.java:9)\n" + "\tat ......remote call(Native Method)\n"
                    + "\tat local.Side.call(Side.java:11)\n" + "\tat local.Main.main(Main.java:1)\n");
    // Suppressed exceptions:
    assertPrintThrowable(
            new Stack("java.lang.IllegalStateException: java.lang.NullPointerException: oops", "p.C.method1:19",
                    "m.Main.main:1")
                            .cause(new Stack("java.lang.NullPointerException: oops", "p.C.method2:23",
                                    "p.C.method1:17", "m.Main.main:1"))
                            .suppressed(
                                    new Stack("java.io.IOException: could not close", "p.C.close:99",
                                            "p.C.method1:18", "m.Main.main:1"),
                                    new Stack("java.io.IOException: java.lang.NullPointerException",
                                            "p.C.flush:77", "p.C.method1:18", "m.Main.main:1")
                                                    .cause(new Stack("java.lang.NullPointerException",
                                                            "p.C.findFlushee:70", "p.C.flush:75",
                                                            "p.C.method1:18", "m.Main.main:1"))),
            "java.lang.IllegalStateException: java.lang.NullPointerException: oops\n"
                    + "\tat p.C.method1(C.java:19)\n" + "\tat m.Main.main(Main.java:1)\n"
                    + "\tSuppressed: java.io.IOException: could not close\n" + "\t\tat p.C.close(C.java:99)\n"
                    + "\t\tat p.C.method1(C.java:18)\n" + "\t\t... 1 more\n"
                    + "\tSuppressed: java.io.IOException: java.lang.NullPointerException\n"
                    + "\t\tat p.C.flush(C.java:77)\n" + "\t\tat p.C.method1(C.java:18)\n" + "\t\t... 1 more\n"
                    + "\tCaused by: java.lang.NullPointerException\n" + "\t\tat p.C.findFlushee(C.java:70)\n"
                    + "\t\tat p.C.flush(C.java:75)\n" + "\t\t... 2 more\n"
                    + "Caused by: java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "\t... 1 more\n",
            "java.lang.NullPointerException: oops\n" + "\tat p.C.method2(C.java:23)\n"
                    + "\tat p.C.method1(C.java:17)\n" + "Also:   java.io.IOException: could not close\n"
                    + "\t\tat p.C.close(C.java:99)\n" + "\t\tat p.C.method1(C.java:18)\n"
                    + "Also:   java.lang.NullPointerException\n" + "\t\tat p.C.findFlushee(C.java:70)\n"
                    + "\t\tat p.C.flush(C.java:75)\n" + "\tCaused: java.io.IOException\n"
                    + "\t\tat p.C.flush(C.java:77)\n" + "\t\tat p.C.method1(C.java:18)\n"
                    + "Caused: java.lang.IllegalStateException\n" + "\tat p.C.method1(C.java:19)\n"
                    + "\tat m.Main.main(Main.java:1)\n");
    // Custom printStackTrace implementations:
    assertPrintThrowable(new Throwable() {
        @Override
        public void printStackTrace(PrintWriter s) {
            s.println("Some custom exception");
        }
    }, "Some custom exception\n", "Some custom exception\n");
    // Circular references:
    Stack stack1 = new Stack("p.Exc1", "p.C.method1:17");
    Stack stack2 = new Stack("p.Exc2", "p.C.method2:27");
    stack1.cause(stack2);
    stack2.cause(stack1);
    assertPrintThrowable(stack1,
            "p.Exc1\n" + "\tat p.C.method1(C.java:17)\n" + "Caused by: p.Exc2\n"
                    + "\tat p.C.method2(C.java:27)\n" + "\t[CIRCULAR REFERENCE:p.Exc1]\n",
            "<cycle to p.Exc1>\n" + "Caused: p.Exc2\n" + "\tat p.C.method2(C.java:27)\n" + "Caused: p.Exc1\n"
                    + "\tat p.C.method1(C.java:17)\n");
}