Interface CheckedRunnable

All Known Subinterfaces:
ExceptionUtil.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
Checked Runnable - Runnable that can throw checked exceptions 可抛出受检异常的 Runnable - 扩展 JDK Runnable 支持受检异常

Extends JDK Runnable to allow throwing checked exceptions in lambdas.

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

Features | 主要功能:

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

Usage Examples | 使用示例:

CheckedRunnable task = () -> Files.delete(tempFile);
Runnable wrapped = task.unchecked();
task.runQuietly();  // 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
    Composes two CheckedRunnables 组合两个 CheckedRunnable
    of(Runnable runnable)
    Wraps a standard Runnable as a CheckedRunnable 将普通 Runnable 包装为 CheckedRunnable
    void
    run()
    Executes an operation that may throw a checked exception 执行操作,可能抛出受检异常
    default void
    Silently executes, ignoring exceptions 静默执行,忽略异常
    default Runnable
    Converts 转换为标准 Runnable,受检异常包装为 RuntimeException
  • Method Details

    • run

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

      default Runnable unchecked()
      Converts 转换为标准 Runnable,受检异常包装为 RuntimeException
      Returns:
      Runnable
    • runQuietly

      default void runQuietly()
      Silently executes, ignoring exceptions 静默执行,忽略异常
    • andThen

      default CheckedRunnable andThen(CheckedRunnable after)
      Composes two CheckedRunnables 组合两个 CheckedRunnable
      Parameters:
      after - the value | 后续操作
      Returns:
      the result | 组合后的 CheckedRunnable
    • of

      static CheckedRunnable of(Runnable runnable)
      Wraps a standard Runnable as a CheckedRunnable 将普通 Runnable 包装为 CheckedRunnable
      Parameters:
      runnable - the value | 普通 Runnable
      Returns:
      CheckedRunnable