【备忘录】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);
}
相关推荐
计算机学姐44 分钟前
基于Python的智能点餐系统【2026最新】
开发语言·vue.js·后端·python·mysql·django·flask
宵时待雨1 小时前
C语言笔记归纳17:数据的存储
c语言·开发语言·笔记
__万波__1 小时前
二十三种设计模式(十)--外观模式
java·设计模式·外观模式
Geoking.1 小时前
深度理解 Java 中的 switch —— 从基础到进阶的完整指南
java
今天你TLE了吗1 小时前
Java:基于注解实现去重表消息防止重复消费
java·spring boot·分布式·spring cloud·幂等
7ioik1 小时前
什么是双亲委派?
开发语言·python
没有bug.的程序员1 小时前
大规模微服务下的 JVM 调优实战指南
java·jvm·spring·wpf·延迟
北友舰长1 小时前
基于Springboot+vue大型商场应急预案管理系统的设计与实现【Java毕业设计·安装调试·代码讲解·文档报告】
java·vue.js·spring boot·mysql·商场·应急处理·应急
赵庆明老师1 小时前
在ASP.NET Core Web Api中添加身份验证和授权
java·前端·asp.net