Module powerunit.exceptions

Package ch.powerunit.extensions.exceptions

Functional interface similar to the once from java.util.function, but with throws Exception.

The various functional interface provided by this package exposes several dedicated methods :

Additionally, it is also possible to override the generated exception (for the uncheck... methods) by specifying a Function to compute the target RuntimeException.

Examples

It is possible to use the method unchecked to directly create a functional interface with only runtime exception :
 FunctionWithException<String, String, IOException> fonctionThrowingException = ...;

 Function<String, String> functionThrowingRuntimeException =
   FunctionWithException.unchecked(fonctionThrowingException);
 
When it is required to thrown a specific RuntimeException, it is also possible to specify it :
 FunctionWithException<String, String, IOException> fonctionThrowingException = ...;

 Function<String, String> functionThrowingRuntimeException =
   FunctionWithException.unchecked(
                                   fonctionThrowingException,
                                   IllegalArgumentException::new
                                  );
 
When the exception should not be thrown in case of error, it is possible to create a Function with Optional result :
 FunctionWithException<String, String, IOException> fonctionThrowingException = ...;

 Function<String, Optional<String>> functionWithOptionalResult =
   FunctionWithException.lifted(fonctionThrowingException);
 
See Also:
java.util.function
Skip navigation links

Copyright © 2019. All rights reserved.