Interface ThrowingRunnable<X extends Throwable>

  • Type Parameters:
    X - The type of checked exception that can be thrown.
    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 ThrowingRunnable<X extends Throwable>
    Represents a task that accepts no input and returns no result. This is a checked-exception throwing equivalent of Runnable.
    • Method Detail

      • run

        void run()
          throws X extends Throwable
        Performs this task.
        Throws:
        X - If an error occurs.
        X extends Throwable
      • onErrorThrowAsChecked

        default <E extends ThrowableThrowingRunnable<E> onErrorThrowAsChecked​(Function<? super X,​? extends E> errorMapper)
        Returns a task that performs this task. Any checked exception thrown by this task is transformed using the given error mapper, and the returned task throws the transformation result.
        Type Parameters:
        E - The type of checked exception to transform to.
        Parameters:
        errorMapper - The function to use to transform any checked exception thrown by this task.
        Returns:
        A task that transforms any thrown checked exception.
        Throws:
        NullPointerException - If errorMapper is null.
      • onErrorThrowAsUnchecked

        default <E extends RuntimeExceptionRunnable onErrorThrowAsUnchecked​(Function<? super X,​? extends E> errorMapper)
        Returns a task that performs this task. Any checked exception thrown by this task is transformed using the given error mapper, and the returned task throws the transformation result.
        Type Parameters:
        E - The type of unchecked exception to transform to.
        Parameters:
        errorMapper - The function to use to transform any checked exception thrown by this task.
        Returns:
        A task that transforms any thrown checked exception.
        Throws:
        NullPointerException - If errorMapper is null.
      • onErrorHandleChecked

        default <E extends ThrowableThrowingRunnable<E> onErrorHandleChecked​(ThrowingConsumer<? super X,​? extends E> errorHandler)
        Returns a task that performs this task. Any checked exception thrown by this task is handled by the given error handler.
        Type Parameters:
        E - The type of checked exception that can be thrown by the given error handler.
        Parameters:
        errorHandler - The operation to perform on any checked exception thrown by this task.
        Returns:
        A task that handles any thrown checked exception.
        Throws:
        NullPointerException - If errorHandler is null.
      • onErrorHandleUnchecked

        default Runnable onErrorHandleUnchecked​(Consumer<? super X> errorHandler)
        Returns a task that performs this task. Any checked exception thrown by this task is handled by the given error handler.
        Parameters:
        errorHandler - The operation to perform on any checked exception thrown by this task.
        Returns:
        A task that handles any thrown checked exception.
        Throws:
        NullPointerException - If errorHandler is null.
      • onErrorRunChecked

        default <E extends ThrowableThrowingRunnable<E> onErrorRunChecked​(ThrowingRunnable<? extends E> fallback)
        Returns a task that performs this task. If this task throws any checked exception, it is discarded and the given fallback task is invoked.
        Type Parameters:
        E - The type of checked exception that can be thrown by the given fallback task.
        Parameters:
        fallback - The task to invoke if this task throws any checked exception.
        Returns:
        A task that invokes the fallback task if this task throws any checked exception.
        Throws:
        NullPointerException - If fallback is null.
      • onErrorRunUnchecked

        default Runnable onErrorRunUnchecked​(Runnable fallback)
        Returns a task that performs this task. If this task throws any checked exception, it is discarded and the given fallback task is invoked.
        Parameters:
        fallback - The task to invoke if this task throws any checked exception.
        Returns:
        A task that invokes the fallback task if this task throws any checked exception.
        Throws:
        NullPointerException - If fallback is null.
      • onErrorDiscard

        default Runnable onErrorDiscard()
        Returns a task that performs this task. Any checked exception thrown by this task is discarded.
        Returns:
        A task that discards any thrown checked exception.
      • of

        static <X extends ThrowableThrowingRunnable<X> of​(ThrowingRunnable<X> task)
        Factory method for turning ThrowingRunnable-shaped lambdas into ThrowingRunnables.
        Type Parameters:
        X - The type of checked exception that can be thrown.
        Parameters:
        task - The lambda to return as ThrowingRunnable.
        Returns:
        The given lambda as a ThrowingRunnable.
        Throws:
        NullPointerException - If task is null.
      • checked

        static <X extends ThrowableThrowingRunnable<X> checked​(Runnable task)
        Returns a task that performs the task task. Any checked exception thrown by the task task is relayed to the caller. This method allows existing Runnable instances to be used where ThrowingRunnable is expected.
        Type Parameters:
        X - The type of checked exception that can be thrown.
        Parameters:
        task - The task to perform when the returned task is performed.
        Returns:
        A task that wraps any checked exception in an UncheckedException.
        Throws:
        NullPointerException - If task is null.
      • checked

        static <X extends ThrowableThrowingRunnable<X> checked​(Runnable task,
                                                                 Class<X> errorType)
        Returns a task that performs the task task to its input. Any UncheckedException thrown by the taks task is unwrapped if its cause is an instance of errorType, otherwise it is relayed to the caller.
        Type Parameters:
        X - The type of checked exception that can be thrown.
        Parameters:
        task - The task to perform when the returned task is performed.
        errorType - The type of checked exception that can be thrown.
        Returns:
        A task that wraps any checked exception in an UncheckedException.
        Throws:
        NullPointerException - If task or errorType is null.