/
ThrowingSupplier.java
54 lines (48 loc) · 1.85 KB
/
ThrowingSupplier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright 2015-2018 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.api.function;
import static org.apiguardian.api.API.Status.STABLE;
import org.apiguardian.api.API;
/**
* {@code ThrowingSupplier} is a functional interface that can be used to
* implement any generic block of code that returns an object and
* potentially throws a {@link Throwable}.
*
* <p>The {@code ThrowingSupplier} interface is similar to
* {@link java.util.function.Supplier}, except that a {@code ThrowingSupplier}
* can throw any kind of exception, including checked exceptions.
*
* <h4>Rationale for throwing {@code Throwable} instead of {@code Exception}</h4>
*
* <p>Although Java applications typically throw exceptions that are instances
* of {@link Exception}, {@link RuntimeException},
* {@link Error}, or {@link AssertionError} (in testing
* scenarios), there may be use cases where a {@code ThrowingSupplier} needs to
* explicitly throw a {@code Throwable}. In order to support such specialized
* use cases, {@link #get} is declared to throw {@code Throwable}.
*
* @since 5.0
* @param <T> the type of argument supplied
* @see java.util.function.Supplier
* @see org.junit.jupiter.api.Assertions#assertTimeout(java.time.Duration, ThrowingSupplier)
* @see org.junit.jupiter.api.Assertions#assertTimeoutPreemptively(java.time.Duration, ThrowingSupplier)
* @see Executable
* @see ThrowingConsumer
*/
@FunctionalInterface
@API(status = STABLE, since = "5.0")
public interface ThrowingSupplier<T> {
/**
* Get a result, potentially throwing an exception.
*
* @return a result
*/
T get() throws Throwable;
}