Returns the string representation of the stack trace of the root cause of the specified exception. - Java java.lang

Java examples for java.lang:Exception StackTrace

Description

Returns the string representation of the stack trace of the root cause of the specified exception.

Demo Code

/*//from   w  w w  .  j a  v a2s .c  o m
 * Copyright (c) 2015-2016 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */
//package com.java2s;

import java.io.PrintWriter;

import java.io.StringWriter;

import java.sql.SQLException;

public class Main {
    /**
     * Returns the string representation of the stack trace of the
     * root cause of the specified exception.
     *
     * @param t an exception.
     * @return the stack trace as a string.
     */
    public static String getRootCauseStackTrace(Throwable t) {
        return getStackTrace(getRootCause(t));
    }

    /**
     * Returns the string representation of the stack trace of the specified exception including
     * all of its causes. This method supports dumping of SQLException chains which do not use the
     * standard chaining mechanism using the {@link Throwable#getCause()} method.
     *
     * @param t an exception.
     * @return the stack trace as a string.
     */
    public static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter w = new PrintWriter(sw);
        w.println(t);

        StackTraceElement[] traceElements = t.getStackTrace();
        for (StackTraceElement traceElement : traceElements)
            w.println("\tat " + traceElement);

        Throwable cause = t.getCause();

        // if the processed exception does not have a standard cause, try to extract
        // cause from "getNextException" in case the processed exception is an SQLException
        if (cause == null && t instanceof SQLException)
            cause = ((SQLException) t).getNextException();

        if (cause != null)
            printStackTraceAsCause(cause, w, traceElements);

        w.flush();
        w.close();

        return sw.toString();
    }

    /**
     * Returns the root cause of the specified exception.
     *
     * @param t an exception.
     * @return the root cause.
     */
    public static Throwable getRootCause(Throwable t) {
        Throwable cause = t;

        while (getCause(cause) != null)
            cause = getCause(cause);

        return cause;
    }

    /**
     * Returns the cause of the specified exception.
     *
     * @param t an exception.
     * @return the cause.
     */
    public static Throwable getCause(Throwable t) {
        // SQLException does not use "standard" cause chaining...grrr
        if (t instanceof SQLException) {
            return ((SQLException) t).getNextException();
        } else {
            return t.getCause();
        }
    }

    /**
     * Recursively dumps the stack trace of the specified cause exception including
     * and all of its cause exceptions to the specified writer. This method handles
     * the SQLException chaining which does not use the standard Java cause chaining.
     *
     * @param t a cause exception.
     * @param w a print writer.
     * @param causedTrace the stack trace of the caused exception (i.e. of the
     * exception caused by t).
     */
    private static void printStackTraceAsCause(Throwable t, PrintWriter w,
            StackTraceElement[] causedTrace) {
        // Compute number of frames in common between this and caused
        StackTraceElement[] trace = t.getStackTrace();

        int m = trace.length - 1;
        int n = causedTrace.length - 1;

        while (m >= 0 && n >= 0 && trace[m].equals(causedTrace[n])) {
            m--;
            n--;
        }
        int framesInCommon = trace.length - 1 - m;

        w.println("Caused by: " + t);
        for (int i = 0; i <= m; i++)
            w.println("\tat " + trace[i]);
        if (framesInCommon != 0)
            w.println("\t... " + framesInCommon + " more");

        // recursion if we have a cause
        Throwable cause = t.getCause();

        // if the processed exception does not have a standard cause, try to extract
        // cause from "getNextException" in case the processed exception is an SQLException
        if (cause == null && t instanceof SQLException)
            cause = ((SQLException) t).getNextException();

        if (cause != null)
            printStackTraceAsCause(cause, w, trace);
    }
}

Related Tutorials