简体   繁体   English

为什么exception.getCause()== exception?

[英]Why is exception.getCause() == exception?

I made a mistake when asking this question by thinking that the value of the member variable cause would be returned as the result of a call to getCause() in org.apache.catalina.connector.ClientAbortException . 在通过认为成员变量cause的值将作为在org.apache.catalina.connector.ClientAbortException调用getCause()的结果而返回时,我在提出这个问题时犯了一个错误。 That obviously isn't the case. 显然情况并非如此。 The real cause is returned. 真正的原因是返回。

Looking at the source of Throwable I can see that it sets cause = this to indicate that the cause has yet to be set. 看看Throwable的来源我可以看到它设置cause = this来表明原因尚未设置。 This was a bad question but I did learn something from asking it. 这是一个糟糕的问题,但我确实从询问中学到了一些东西。

Original question below: 原始问题如下:

Sometimes when you catch exceptions, the getCause() will return the exception object itself, making it recursive. 有时当你捕获异常时,getCause()将返回异常对象本身,使其递归。 Why doesn't getCause() return null? 为什么getCause()不返回null?

getCause() should never return itself unless it's been overridden - if you look at the code for getCause() it explicitly returns null if the cause variable is the same as the throwable you're calling it on. 除非被覆盖,否则getCause()永远不会返回自己 - 如果你查看getCause()的代码,如果cause变量与你调用它的throwable相同,它显式返回null

Which exceptions are you running into which do return the same exception again? 哪些异常,你运行成再次返回相同的异常?

Can you give an example of when this occurs? 你能举例说明何时发生这种情况? The only way I can see that it would happen is if the author called initCause() with the exception itself after construction, and this is very bad practive. 我可以看到它会发生的唯一方法是,如果作者在构造之后调用initCause()并且异常本身,这是非常糟糕的。 I've never seen it. 我从未见过它。

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

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