Interface CheckedConsumer<T>

Type Parameters:
T - input type - 输入类型
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface CheckedConsumer<T>
Checked Consumer - Consumer that can throw checked exceptions 可抛出受检异常的 Consumer - 扩展 JDK Consumer 支持受检异常

Extends JDK Consumer to allow throwing checked exceptions in lambdas.

扩展 JDK Consumer,支持在 lambda 中抛出受检异常。

Features | 主要功能:

  • Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
  • Convert to standard Consumer (unchecked) - 转换为标准 Consumer
  • Silent execution (acceptQuietly) - 静默执行
  • Composition with andThen - 组合操作

Usage Examples | 使用示例:

CheckedConsumer<Path> consumer = path -> Files.delete(path);
Consumer<Path> wrapped = consumer.unchecked();
consumer.acceptQuietly(path);  // ignores exceptions

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(T t)
    Executes an operation that may throw a checked exception 执行操作,可能抛出受检异常
    default void
    Silently executes, ignoring exceptions 静默执行,忽略异常
    andThen(CheckedConsumer<? super T> after)
    Composes two CheckedConsumers 组合两个 CheckedConsumer
    static <T> CheckedConsumer<T>
    of(Consumer<T> consumer)
    Wraps a standard Consumer as a CheckedConsumer 将普通 Consumer 包装为 CheckedConsumer
    default Consumer<T>
    Converts 转换为标准 Consumer,受检异常包装为 RuntimeException
  • Method Details

    • accept

      void accept(T t) throws Exception
      Executes an operation that may throw a checked exception 执行操作,可能抛出受检异常
      Parameters:
      t - the value | 输入值
      Throws:
      Exception - if the condition is not met | 如果操作失败
    • unchecked

      default Consumer<T> unchecked()
      Converts 转换为标准 Consumer,受检异常包装为 RuntimeException
      Returns:
      Consumer
    • acceptQuietly

      default void acceptQuietly(T t)
      Silently executes, ignoring exceptions 静默执行,忽略异常
      Parameters:
      t - the value | 输入值
    • andThen

      default CheckedConsumer<T> andThen(CheckedConsumer<? super T> after)
      Composes two CheckedConsumers 组合两个 CheckedConsumer
      Parameters:
      after - the value | 后续操作
      Returns:
      the result | 组合后的 CheckedConsumer
    • of

      static <T> CheckedConsumer<T> of(Consumer<T> consumer)
      Wraps a standard Consumer as a CheckedConsumer 将普通 Consumer 包装为 CheckedConsumer
      Type Parameters:
      T - the value | 输入类型
      Parameters:
      consumer - the value | 普通 Consumer
      Returns:
      CheckedConsumer