Java 如何在捕获的异常上显示堆栈跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2245088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:11:49  来源:igfitidea点击:

How to display stack trace on a caught exception?

javatomcatstack-trace

提问by ufk

I have a generic function that prints exceptions (using log4j):

我有一个打印异常的通用函数(使用 log4j):

private void _showErrorMessage(Exception e) {
    log.error(e.getClass() + ": " +  e.getMessage() + ": " + e.getCause() + "\n" +  e.getStackTrace().toString());
}

Instead of seeing the stack trace I'm seeing:

而不是看到我看到的堆栈跟踪:

[Ljava.lang.StackTraceElement;@49af7e68

How can I view the stack trace of the exception properly?

如何正确查看异常的堆栈跟踪?

update

更新

log.error(e) <- shows the error, but doesn't show stack trace

log.error(e) <- 显示错误,但不显示堆栈跟踪

回答by coobird

Throwable.getStackTracereturns an array of StackTraceElements, hence the toStringmethod is returning a textual representation of the array itself.

Throwable.getStackTrace返回一个StackTraceElements数组,因此该toString方法返回数组本身的文本表示。

In order to actually retrieve the stack trace information, one would have to go through each StackTraceElementto get more information.

为了实际检索堆栈跟踪信息,必须遍历每个StackTraceElement以获取更多信息。

回答by Joachim Sauer

Your logging framework should have the ability to log exceptions, so simply passing the exception to the proper .error(Object, Throwable)call should be enough:

您的日志框架应该能够记录异常,因此只需将异常传递给正确的.error(Object, Throwable)调用就足够了:

If your logging framework can't do that, or you need the stack trace in a Stringfor any other reason, then it becomes a bit harder. You'll have to create a PrintWriterwrapping a StringWriterand call .printStackTrace()on the Exception:

如果您的日志框架无法做到这一点,或者您String出于任何其他原因需要 a 中的堆栈跟踪,那么它会变得有点困难。你必须创建一个PrintWriter包裹一个StringWriter,并呼吁.printStackTrace()Exception

StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();

回答by Paul McKenzie

Have you tried?

你有没有尝试过?

private void _showErrorMessage(Exception e) {
    log.error("Hey! got an exception", e);
}

回答by gpampara

You could also look at the Guava libraries from Google.

您还可以查看 Google 的 Guava 库。

Throwables.getStackTraceAsString(Throwable throwable)

Throwables.getStackTraceAsString(Throwable throwable)

回答by Yishai

The exact answer to your question is that you should call Log4J like this:

您问题的确切答案是您应该像这样调用 Log4J:

private void _showErrorMessage(Exception e) {
    log.error(e.getClass() + ": " +  e.getMessage() + ": " + e.getCause(), e);
}

Although I would dispense with the call to e.getCause() because the stacktrace will give that to you anyway, so:

虽然我会免除对 e.getCause() 的调用,因为无论如何堆栈跟踪都会给你,所以:

private void _showErrorMessage(Exception e) {
    log.error(e.getClass() + ": " +  e.getMessage(), e);
}

ExceptionUtils is fine if you really need a string of the stacktrace, but since you are using Log4J, you lose a lot by not utilizing its built in exception handling.

如果您确实需要一串堆栈跟踪,ExceptionUtils 很好,但是由于您使用的是 Log4J,如果不利用其内置的异常处理,您会损失很多。

回答by Marco C.

Exception Stacktrace loggingshows two methods for this purpose, one based on Apache Commons and another using the standard JDK method.

异常堆栈跟踪日志显示了两种用于此目的的方法,一种基于 Apache Commons,另一种使用标准 JDK 方法。