Java 打印 Exception 与 Exception.getMessage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32840399/
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-11 13:11:46  来源:igfitidea点击:

Printing Exception vs Exception.getMessage

javaexception

提问by DesirePRG

Is there a best practice on using the followng two pieces of code regarding exceptions.

是否有使用以下两段有关异常的代码的最佳实践。

//code1

} catch (SomeException e) {
    logger.error("Noinstance available!", e.getMessage());
}

//code2
} catch (SomeException e) {
    logger.error("Noinstance available!", e);
}

When should I use the getMessage method of an exception?

什么时候应该使用异常的 getMessage 方法?

采纳答案by Davide Lorenzo MARINO

The first doesn't compile because the method erroraccept a Stringas first parameter and a Throwableas second parameter.

第一个不编译,因为该方法error接受一个String作为第一个参数和一个Throwable作为第二个参数。

e.getMessage()is not a Throwable.

e.getMessage()不是Throwable.

The code should be

代码应该是

} catch (SomeException e) {
    // No stack trace
    logger.error("Noinstance available! " + e.getMessage());
}

Compared with

和....相比

} catch (SomeException e) {
    // Prints message and stack trace
    logger.error("Noinstance available!", e);
}

The first prints only a message. The second prints also the whole stack trace.

第一个只打印一条消息。第二个也打印整个堆栈跟踪。

It depends from the context if it is necessary to print the stack trace or not.

是否有必要打印堆栈跟踪取决于上下文。

If you already know why an exception can be thrown it is not a good idea to print the whole stack trace.

如果您已经知道为什么会抛出异常,那么打印整个堆栈跟踪并不是一个好主意。

If you don't know, it is better to print the whole strack trace to find easily the error.

如果您不知道,最好打印整个 strack 跟踪以轻松查找错误。

回答by James

Exception.printStackTrace()should not be used unless youre for example debugging.

Exception.printStackTrace()除非您正在调试,否则不应使用。

For logging, it is definetly better to use Exception.getMessage(). Also please notice that many logging frameworks (for example Log4j) accepts exception it self as an argument on logging method (for example error()) and works it out by itself.

对于日志记录,使用Exception.getMessage(). 另请注意,许多日志框架(例如 Log4j)接受异常本身作为日志方法(例如error())的参数并自行解决。

回答by zdenda.online

It depends if you need stack trace of original exception (SomeException). If yes, then code2 is correct option here. Note that is more common way of handling these situations.

这取决于您是否需要原始异常(SomeException)的堆栈跟踪。如果是,那么这里的 code2 是正确的选项。请注意,这是处理这些情况的更常见方法。

If you are not interested in what was the original exception, you can use code1 but this one is not correct. Because the code you sent takes message as argument. So the correct way is:

如果您对原始异常不感兴趣,您可以使用 code1,但这是不正确的。因为您发送的代码将消息作为参数。所以正确的做法是:

logger.error("Noinstance available! - reason:" + e.getMessage());

回答by rajuGT

This prototype is useful, when you classify exception into Business leveland Technical exception.

当您将异常分类为Business levelTechnical exception时,此原型很有用。

For Business level exception, you just use the message, by logging for analyticsor may be to display on the screen a meaning information (something didnt work).

对于业务级别的异常,您只需使用消息,通过记录 分析或可能在屏幕上显示一个有意义的信息(有些东西不起作用)。

For Technical exception, its better to log the error with stacktrace, to find of out the issue and it is easy to debug and resolve.

对于技术异常,最好使用堆栈跟踪记录错误,以找出问题并且易于调试和解决。

回答by SKerkewitz

With stacktrace:

使用堆栈跟踪:

logger.error("Could not do XYZ because of: " + ex, ex);

Without stacktrace:

没有堆栈跟踪:

logger.error("Could not do XYZ because of: " + ex);

回答by Chetan chadha

You can try this:-

你可以试试这个:-

logger.error(e.getMessage(), e);

It Returns the detail message string of this throwable.

它返回此 throwable 的详细消息字符串。

logger.error(e.getLocalizedMessage(),e);

Creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().

创建此 throwable 的本地化描述。子类可以覆盖此方法以生成特定于语言环境的消息。对于不覆盖此方法的子类,默认实现返回与 getMessage() 相同的结果。

In your case e is nothing but the object of exception ...

在你的情况下, e 只不过是例外的对象......

getLocalizedMessage() u need to override and give your own message i.e the meaning for localized message.

getLocalizedMessage() 您需要覆盖并提供您自己的消息,即本地化消息的含义。