集册 Java实例教程 以递归方式将指定原因异常的堆栈跟踪(包括及其所有原因异常)转储到指定编写器。

以递归方式将指定原因异常的堆栈跟踪(包括及其所有原因异常)转储到指定编写器。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

546
以递归方式将指定原因异常的堆栈跟踪(包括及其所有原因异常)转储到指定编写器。

/*

 * Copyright (c) 2015-2016 QuartzDesk.com.

 * Licensed under the MIT license (https://opensource.org/licenses/MIT).

 *//*from 时代Java公众号 - nowjava.com*/

//package com.nowjava;


import java.io.PrintWriter;


import java.io.StringWriter;


import java.sql.SQLException;


public class Main {

    /**

     * 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--;/**来自 时代Java公众号 - N o w J a  v a . c o m**/

        }

        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);

    }


    /**

     * 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.fl
展开阅读全文