gpt4 book ai didi

java - Java RuntimeException enableSuppression 参数有什么作用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:32 24 4
gpt4 key购买 nike

我不知道怎么说我已经使用 Java 4 年了,这是我第一次遇到这个参数 enableSuppression 谁能解释一下它的作用以及何时使用启用/禁用它?

最佳答案

enableSuppressionThrowable(包括Exception)的构造函数中的参数

判断是否开启抑制。

来自 Javadocs :

The suppression behavior is enabled unless disabled via a constructor.

Note that when one exception causes another exception, the first exception is usually caught and then the second exception is thrown in response. In other words, there is a causal connection between the two exceptions. In contrast, there are situations where two independent exceptions can be thrown in sibling code blocks, in particular in the try block of a try-with-resources statement and the compiler-generated finally block which closes the resource. In these situations, only one of the thrown exceptions can be propagated. In the try-with-resources statement, when there are two such exceptions, the exception originating from the try block is propagated and the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.

An exception may have suppressed exceptions while also being caused by another exception. Whether or not an exception has a cause is semantically known at the time of its creation, unlike whether or not an exception will suppress other exceptions which is typically only determined after an exception is thrown.

Note that programmer written code is also able to take advantage of calling this method in situations where there are multiple sibling exceptions and only one can be propagated.

关于java - Java RuntimeException enableSuppression 参数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com