Interface Try<T>
- Type Parameters:
T- type of Try's success result.
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
Try.Failure,Try.Success
Try can be either success with value of type T or failure with an exception.
In some way Try is similar to Optional which may have value or not.
Use isSuccess() and isFailure() methods to determine flavor of current try.
Try has static factory methods Try.of(...) for producing tries from
Try.CheckedSupplier, Try.CheckedRunnable functional interfaces.
Methods that return Try could be used for method chaining.
Try.of(...) .map(...) .map(...) .filter(...) .recover(...) .recover(...) .onFailure(...) .optional() .get(...)
Be aware that these Try methods behave differently depending on success or failure.
For example onSuccess(CheckedConsumer) method does not have any effect
in case of failure and onFailure(Consumer) does not have any effect for success.
When Try becomes failure, the only way to get back to success is recover method.
Try has a set of recover(...) methods to recover failed try to the success.
You can invoke recover many times implementing different recover strategies.
If some recover method succeeds, then remaining recover methods
will not have an action/effect.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfacestatic interfacestatic interfacestatic interfacestatic classTry's failure projection.static classTry's success projection. -
Method Summary
Modifier and TypeMethodDescriptionandFinally(Try.CheckedRunnable runnable) Behaves like finally block in Java's try/catch/finally.catching(Try.CheckedFunction<T, R> func) Higher order function to transform partialT->Rfunction to the totalT->Try<R>function.voidclose()consumeCatching(Try.CheckedConsumer<T> cons) static <T> Try<T>Factory method to produce Try from value of Exception.Filters current Try, has no action for failure.default <R> Try<R>Maps Try of type T to try of type R<R> Rdefault Tget()Synonym fororElseThrow()getCatching(Try.CheckedSupplier<T> supl) default booleanChecks if it is failure try.default booleanChecks if it is success try.default <R> Try<R>map(Try.CheckedFunction<? super T, ? extends R> mapper) Maps Try of type T to type Rof(Try.CheckedRunnable runnable) Factory method to produce Try from runnable that may throw an exception.static <T> Try<T>of(Try.CheckedSupplier<? extends T> supplier) Factory method to produce Try from supplier that may throw an exception.Executes action on failure, has no action for success.onSuccess(Try.CheckedConsumer<? super T> consumer) Executes action on success, has no action for failure.optional()Gets Try's value or throws exceptionpeek(Try.CheckedConsumer<Try<T>> consumer) Gives access to current Try.recover(Try.CheckedSupplier<? extends T> supplier) Tries recover failed try with given supplier, has no action for success.recover(Try.CheckedSupplier<? extends T> supplier, Predicate<Exception> predicate) Tries recover failed try with given supplier, has no action for success.stream()static <T> Try<T>success(T value) Factory method to produce Try from value of T type.static <T> Try.CheckedFunction<T,T> toFunction(Try.CheckedConsumer<? super T> cons) static <U> Try.CheckedFunction<Object,U> toFunction(Try.CheckedSupplier<? extends U> supplier) Converts supplier to function.
-
Method Details
-
onSuccess
Executes action on success, has no action for failure.- Parameters:
consumer- consumer- Returns:
- this Try or new failure is consumer throws an exception
-
onFailure
Executes action on failure, has no action for success.- Parameters:
consumer- consumer- Returns:
- this Try or new failure is consumer throws an exception
-
isSuccess
default boolean isSuccess()Checks if it is success try.- Returns:
- true if it is success.
-
recover
Tries recover failed try with given supplier, has no action for success.- Parameters:
supplier- supplier to recover- Returns:
- this Try for success, new success or new failure depending on if supplier had thrown exception.
-
recover
Tries recover failed try with given supplier, has no action for success.- Parameters:
supplier- supplier to recoverpredicate- recover attempt happens if predicate returns true.- Returns:
- this Try for success, new success or new failure depending on if supplier had thrown exception.
-
map
Maps Try of type T to type R- Type Parameters:
R- new result type- Parameters:
mapper- mapper- Returns:
- new Try of R type or failure if mapper throws exception/
-
flatMap
Maps Try of type T to try of type R- Type Parameters:
R- new result type- Parameters:
mapper- mapper- Returns:
- new Try of R type or failure if mapper throws exception/
-
filter
Filters current Try, has no action for failure.- Parameters:
predicate- predicate to test- Returns:
- this Try if predicate returns true or new fail with
NoSuchElementException
-
stream
-
optional
-
close
void close()- Specified by:
closein interfaceAutoCloseable
-
orElseThrow
T orElseThrow()Gets Try's value or throws exception- Returns:
- value of T in case of success
- Throws:
RuntimeException- in case of failure
-
fold
-
isFailure
default boolean isFailure()Checks if it is failure try.- Returns:
- true if failure.
-
get
Synonym fororElseThrow()- Returns:
- see
orElseThrow()
-
andFinally
Behaves like finally block in Java's try/catch/finally. Will executed for both cases - either success or failure.- Parameters:
runnable- runnable to execute- Returns:
- this Try or new failure if runnable throws exception.
-
peek
Gives access to current Try.- Parameters:
consumer- Try's consumer- Returns:
- this Try or failure if consumer throws an exception.
-
success
Factory method to produce Try from value of T type.- Type Parameters:
T- result type- Parameters:
value- success value- Returns:
- Try <T>
-
failure
Factory method to produce Try from value of Exception.- Type Parameters:
T- result type- Parameters:
exception- exception- Returns:
- Try <T>
-
of
Factory method to produce Try from supplier that may throw an exception.- Type Parameters:
T- Try's result type- Parameters:
supplier- that gives the result of T type and may throw an exception.- Returns:
- Try of T type
- Throws:
NullPointerException- if supplier returns null.
-
of
Factory method to produce Try from runnable that may throw an exception.- Parameters:
runnable- exceptional runnable- Returns:
- Try of
Voidtype
-
catching
Higher order function to transform partialT->Rfunction to the totalT->Try<R>function.
Simplifies using Try with Java's streams and optionals. Sample usage:Stream.of("1", "2") //Stream<String> .map(Try.lift(Integer::valueOf) //Stream<Try<Integer>> .flatMap(Try::stream) //Stream<Integer> .toList() //List<Integer>- Type Parameters:
T- function parameter typeR- function result type- Parameters:
func- partial functionT->R- Returns:
- total function
T->Try<R>
-
consumeCatching
-
getCatching
-
runCatching
-
toFunction
-
toFunction
Converts supplier to function.
-