【备忘录】java.lang.Throwable#addSuppressed这个是干嘛的?

复制代码
public void onFailure(Exception e) {
    try {
        delegate.onFailure(e);
    } catch (RuntimeException ex) {
        if (ex != e) {
            ex.addSuppressed(e);
        }
        assert false : new AssertionError("listener.onFailure failed", ex);
        throw ex;
    }
}

Appends the specified exception to the exceptions that were suppressed in order to deliver this exception. This method is thread-safe and typically called (automatically and implicitly) by the try-with-resources statement.

The suppression behavior is enabled unless disabled via a constructor. When suppression is disabled, this method does nothing other than to validate its argument.

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.

复制代码
public final synchronized void addSuppressed(Throwable exception) {
    if (exception == this)
        throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);

    Objects.requireNonNull(exception, NULL_CAUSE_MESSAGE);

    if (suppressedExceptions == null) // Suppressed exceptions not recorded
        return;

    if (suppressedExceptions == SUPPRESSED_SENTINEL)
        suppressedExceptions = new ArrayList<>(1);

    suppressedExceptions.add(exception);
}
相关推荐
喆星时瑜1 分钟前
Win10/11 安装 R 与 RStudio 详细图文教程
开发语言·r语言·安装
心中有你021417 分钟前
【路径规划】A*寻路算法最通俗易懂讲解(C语言完整实现+详细注释)
c语言·开发语言·算法
言乐61 小时前
Python自动去除水印
开发语言·python·django·virtualenv·pygame
大圣编蚕1 小时前
C语言入门:基本语法汇总
c语言·开发语言
czq_26867194871 小时前
Python打卡第31天
开发语言·python
invicinble1 小时前
python 编程语言 认识维度
开发语言·数据库·python
AC赳赳老秦1 小时前
数据采集全链路审计留痕:用 OpenClaw 实现合规审计与追溯
开发语言·汇编·python·php·swift·deepseek·openclaw
czhc11400756631 小时前
退出防呆:用“总闸“收口所有退出入口,以及三个并发语法坑
开发语言·c#
三8441 小时前
Fastjson 漏洞 · 02 · autoType 机制与 checkAutoType
java·fastjson
悠仁さん2 小时前
【C++】vector 类
开发语言·c++