Package fr.enedis.chutney.tools
Interface ThrowingSupplier<T,E extends Exception>
- Type Parameters:
T- The type of the result suppliedE- The type of exception thrown by the supplier
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A version of
Supplier that allows throwing a checked Exception.
This interface is useful when working with functional programming constructs that need to handle checked exceptions, such as stream operations or deferred computations.
Usage Example
ThrowingSupplier<String, IOException> fileReader = () -> {
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
return reader.readLine();
}
};
try {
String line = fileReader.get();
System.out.println("Read line: " + line);
} catch (IOException e) {
e.printStackTrace();
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionget()silence(ThrowingSupplier<T, E> throwingFunction, Function<Exception, T> exceptionHandler) toUnchecked(ThrowingSupplier<T, E> throwingFunction) default T
-
Method Details
-
get
- Throws:
E
-
unsafeGet
-
toUnchecked
static <T,E extends Exception> Supplier<T> toUnchecked(ThrowingSupplier<T, E> throwingFunction) throws UncheckedException- Throws:
UncheckedException- if givenThrowingFunctionthrows
-
silence
static <T,E extends Exception> Supplier<T> silence(ThrowingSupplier<T, E> throwingFunction, Function<Exception, T> exceptionHandler)
-