【备忘录】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);
}
相关推荐
外收内放12 分钟前
FastAPI猜字谜实战(3):把每一局存成JSON
开发语言·python·学习·json·fastapi
denlaku12 分钟前
JDK 20 新特性详解
java
tedcloud12314 分钟前
Docker Compose 部署 Memos:从安装到备份恢复
linux·开发语言·jvm·后端·docker·容器·rust
镜子AI15 分钟前
镜子AI案例工程复盘:用Python给GEO效果做抽样检测与置信区间估计
开发语言·人工智能·python
木井巳18 分钟前
【JavaEE】SpringBoot 统一功能处理: 拦截器、统一返回与全局异常
java·spring boot·后端·spring·java-ee
春涧草茶20 分钟前
慢就是快15-1 多进程 多线程
开发语言·python
pan_junbiao20 分钟前
Go语言中的空白标识符
开发语言·后端·golang
SL-staff21 分钟前
JVS-Rules金融风控实践:四大原生能力的技术实现与可落地配置指南
java·决策树·规则引擎·函数计算·金融风控·jvs-rules·数据源抽象
卡卡~~27 分钟前
C++学习
开发语言·c++
qetfw42 分钟前
Bash 基础:脚本执行、引号与命令替换验证
linux·开发语言·chrome·bash