Interface CheckedRunnable

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 CheckedRunnable
CheckedRunnable - Runnable that may throw checked exceptions CheckedRunnable - 可能抛出受检异常的可运行接口

Extends the concept of JDK Runnable to allow throwing checked exceptions. Provides safe conversion to standard Runnable via unchecked() and silent execution via runQuietly().

扩展 JDK Runnable 的概念,支持抛出受检异常。 通过 unchecked() 安全转换为标准 Runnable, 通过 runQuietly() 静默执行。

Features | 主要功能:

  • Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
  • Convert to standard Runnable (unchecked) - 转换为标准 Runnable
  • Silent execution - 静默执行
  • Chaining support (andThen) - 支持链式调用

Usage Examples | 使用示例:

CheckedRunnable closeResources = () -> connection.close();

Runnable wrapped = closeResources.unchecked();
closeResources.runQuietly();

// Chaining
closeResources.andThen(() -> log.info("Resources closed"))
    .run();

Security | 安全性:

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

    Modifier and Type
    Method
    Description
    Chain with another runnable (execute this, then after) 与另一个可运行接口链接(先执行本操作,再执行 after)
    of(Runnable runnable)
    Wrap a standard Runnable as CheckedRunnable 将标准 Runnable 包装为 CheckedRunnable
    void
    run()
    Run the operation, may throw checked exception 运行操作,可能抛出受检异常
    default void
    Run silently, ignoring any checked or unchecked exception (but not Error) 静默运行,忽略任何受检或非受检异常(不包括 Error
    default Runnable
    Convert to standard Runnable, wrapping checked exceptions in OpenFunctionalException 转换为标准 Runnable,将受检异常包装为 OpenFunctionalException
  • Method Details

    • run

      void run() throws Exception
      Run the operation, may throw checked exception 运行操作,可能抛出受检异常
      Throws:
      Exception - if operation fails - 如果操作失败
    • unchecked

      default Runnable unchecked()
      Convert to standard Runnable, wrapping checked exceptions in OpenFunctionalException 转换为标准 Runnable,将受检异常包装为 OpenFunctionalException

      Runtime exceptions are rethrown as-is; checked exceptions are wrapped in OpenFunctionalException.

      运行时异常原样抛出;受检异常被包装为 OpenFunctionalException

      Returns:
      Runnable that wraps checked exceptions - 包装受检异常的 Runnable
    • runQuietly

      default void runQuietly()
      Run silently, ignoring any checked or unchecked exception (but not Error) 静默运行,忽略任何受检或非受检异常(不包括 Error
    • andThen

      default CheckedRunnable andThen(CheckedRunnable after)
      Chain with another runnable (execute this, then after) 与另一个可运行接口链接(先执行本操作,再执行 after)
      Parameters:
      after - runnable to execute after this - 在本操作之后执行的可运行接口
      Returns:
      chained runnable - 链式可运行接口
      Throws:
      NullPointerException - if after is null - 如果 after 为 null
    • of

      static CheckedRunnable of(Runnable runnable)
      Wrap a standard Runnable as CheckedRunnable 将标准 Runnable 包装为 CheckedRunnable
      Parameters:
      runnable - standard Runnable - 标准 Runnable
      Returns:
      CheckedRunnable wrapper - CheckedRunnable 包装器