Interface ThrowingSupplier<T>

  • Type Parameters:
    T - the type of argument supplied
    All Superinterfaces:
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    public interface ThrowingSupplier<T>
    extends Executable
    ThrowingSupplier is a functional interface that can be used to implement any generic block of code that returns an object and potentially throws a Throwable.

    The ThrowingSupplier interface is similar to Supplier, except that a ThrowingSupplier can throw any kind of exception, including checked exceptions.

    As of JUnit Jupiter 5.3, ThrowingSupplier extends Executable, providing a default implementation of execute() that delegates to get() and ignores the return value. This allows the Java compiler to disambiguate between ThrowingSupplier and Executable when performing type inference for a lambda expression or method reference supplied to an overloaded method that accepts either a ThrowingSupplier or an Executable.

    Rationale for throwing Throwable instead of Exception

    Although Java applications typically throw exceptions that are instances of Exception, RuntimeException, Error, or AssertionError (in testing scenarios), there may be use cases where a ThrowingSupplier needs to explicitly throw a Throwable. In order to support such specialized use cases, get() is declared to throw Throwable.

    See Also:
    Supplier, Assertions.assertTimeout(java.time.Duration, ThrowingSupplier), Assertions.assertTimeoutPreemptively(java.time.Duration, ThrowingSupplier), Executable, ThrowingConsumer