获取完整的字符串堆栈跟踪,包括内部异常

发布于 2024-08-02 08:15:07 字数 220 浏览 4 评论 0原文

Java 的 e.printStackTrace() 不会打印内部异常堆栈跟踪的所有详细信息。

有没有现成的方法可以生成字符串形式的完整堆栈跟踪? (除了我自己格式化它)

编辑

我刚刚发现了 printStackTrace() 的作用 - 显然它过滤掉的堆栈帧正是内部异常和外部异常所共有的。所以实际上这正是我想要的,而不是“完整”堆栈跟踪。

Java's e.printStackTrace() doesn't print all the details of the inner exception's stack trace.

Is there a ready way to generate the complete stack trace in string form? (besides formatting it myself)

Edit

I just found out what printStackTrace() does - apparently the stack frames it filters out are exactly the ones common to the inner exception and the outer one. So in fact it is rather what I want, and not the 'full' stack trace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2024-08-09 08:15:07

I suggest that you use the ExceptionUtils class from Apache Commons lang, which provides useful method for that.

黯然 2024-08-09 08:15:07

我最终推出了自己的版本(我采用了 Throwable.printStackTrace() 的实现并对其进行了一些调整):

public static String joinStackTrace(Throwable e) {
    StringWriter writer = null;
    try {
        writer = new StringWriter();
        joinStackTrace(e, writer);
        return writer.toString();
    }
    finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e1) {
                // ignore
            }
    }
}

public static void joinStackTrace(Throwable e, StringWriter writer) {
    PrintWriter printer = null;
    try {
        printer = new PrintWriter(writer);

        while (e != null) {

            printer.println(e);
            StackTraceElement[] trace = e.getStackTrace();
            for (int i = 0; i < trace.length; i++)
                printer.println("\tat " + trace[i]);

            e = e.getCause();
            if (e != null)
                printer.println("Caused by:\r\n");
        }
    }
    finally {
        if (printer != null)
            printer.close();
    }
}

I ended up rolling my own (I took the implementation of Throwable.printStackTrace() and tweaked it a bit):

public static String joinStackTrace(Throwable e) {
    StringWriter writer = null;
    try {
        writer = new StringWriter();
        joinStackTrace(e, writer);
        return writer.toString();
    }
    finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e1) {
                // ignore
            }
    }
}

public static void joinStackTrace(Throwable e, StringWriter writer) {
    PrintWriter printer = null;
    try {
        printer = new PrintWriter(writer);

        while (e != null) {

            printer.println(e);
            StackTraceElement[] trace = e.getStackTrace();
            for (int i = 0; i < trace.length; i++)
                printer.println("\tat " + trace[i]);

            e = e.getCause();
            if (e != null)
                printer.println("Caused by:\r\n");
        }
    }
    finally {
        if (printer != null)
            printer.close();
    }
}
小兔几 2024-08-09 08:15:07

是的,您可以使用 Throwable.getStackTrace() 返回的 StackTraceElement 类并查找详细信息。

从API:

数组的最后一个元素(假设数组的长度是
非零)代表栈底,也就是第一个方法
按顺序调用。

Yes you can use the StackTraceElement class returned by Throwable.getStackTrace() and find the details.

From the API:

The last element of the array (assuming the array's length is
non-zero) represents the bottom of the stack, which is the first method
invocation in the sequence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文