【备忘录】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);
}
相关推荐
charlie1145141911 分钟前
现代嵌入式C++教程:对象池(Object Pool)模式
开发语言·c++·学习·算法·嵌入式·现代c++·工程实践
小北方城市网2 分钟前
接口性能优化实战:从秒级到毫秒级
java·spring boot·redis·后端·python·性能优化
小北方城市网4 分钟前
Redis 缓存设计与避坑实战:解决穿透 / 击穿 / 雪崩
java·大数据·数据库·redis·python·elasticsearch·缓存
jiayong234 分钟前
MINA框架面试题 - 进阶篇
java·io·mina
TTGGGFF7 分钟前
控制系统建模仿真(二):掌握控制系统设计的 MAD 流程与 MATLAB 基础运算
开发语言·数据结构·matlab
鸡蛋豆腐仙子10 分钟前
Spring的AOP失效场景
java·后端·spring
郑州光合科技余经理13 分钟前
O2O上门预约小程序:全栈解决方案
java·大数据·开发语言·人工智能·小程序·uni-app·php
roo_114 分钟前
JAVA学习-MAC搭建java环境和spring boot搭建
java·学习·macos
小北方城市网15 分钟前
SpringBoot 全局异常处理最佳实践:从混乱到规范
java·spring boot·后端·spring·rabbitmq·mybatis·java-rabbitmq
潇凝子潇18 分钟前
在 Maven 中跳过单元测试进行本地打包或排除某个项目进行打包
java·单元测试·maven