简体   繁体   English

链式异常与PrintStackTrace

[英]Chained Exception versus PrintStackTrace

Chained Exception is useful in cases in which knowledge of an underlying cause is useful. 在了解根本原因的情况下,链接异常非常有用。 The Throwable getCause( )method returns the exception that underlies the current exception. Throwable getCause()方法返回作为当前异常基础的异常。

On the other hand, if we call PrintStackTrace() in the catch block, we will have the entire logs printed in the logger file(and in eclipse console). 另一方面,如果在catch块中调用PrintStackTrace(),则将在日志文件(和eclipse控制台)中打印所有日志。 The detailed log will show the exception occurred, and the underlying chain of exceptions as well. 详细的日志将显示发生的异常以及异常的基础链。

So, what is the primary difference in the usages of both of them. 因此,两者的用法的主要区别是什么。

One approach is to provide debug information (PrintStackTrace). 一种方法是提供调试信息(PrintStackTrace)。 The other approach is for actual flow control (Throwable.getCause()). 另一种方法是用于实际流控制(Throwable.getCause())。

(Usually) the underlying cause is considered an implementation detail, and therefore should not be used for flow control. (通常)将根本原因视为实现细节,因此不应将其用于流量控制。 Add the information you need to the main exception or create multiple types for exception. 将所需的信息添加到主要异常或为异常创建多种类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM