public abstract class Try<T>
| Constructor and description |
|---|
protected Try
() |
| Type Params | Return Type | Name and description |
|---|---|---|
<U> |
public static Try<U> |
failure(Throwable e) |
|
public abstract Try<T> |
filter(Predicate<T> pred)If a Try is a Success and the predicate holds true, the Success is passed further. |
<U> |
public abstract Try<U> |
flatMap(TryMapFunction<? super T, Try<U>> f)Transform success or pass on failure, taking a Try<U> as the result. |
|
public abstract T |
get()Gets the value T on Success or throws the cause of the failure. |
|
public abstract T |
getUnchecked()Gets the value T on Success or throws the cause of the failure wrapped into a RuntimeException |
|
public abstract boolean |
isSuccess() |
<U> |
public abstract Try<U> |
map(TryMapFunction<? super T, ? extends U> f) |
<U> |
public static Try<U> |
ofFailable(TrySupplier<U> f) |
<E extends Throwable> |
public abstract Try<T> |
onFailure(TryConsumer<Throwable, E> action)Performs the provided action, when failed |
<E extends Throwable> |
public abstract Try<T> |
onSuccess(TryConsumer<T, E> action)Performs the provided action, when successful |
|
public abstract T |
orElse(T value)Return a value in the case of a failure. |
<X extends Throwable> |
public abstract T |
orElseThrow(Supplier<? extends X> exceptionSupplier) |
|
public abstract Try<T> |
orElseTry(TrySupplier<T> f)Return another try in the case of failure. |
|
public abstract T |
recover(Function<? super Throwable, T> f) |
|
public abstract Try<T> |
recoverWith(TryMapFunction<? super Throwable, Try<T>> f)Try applying f(t) on the case of failure. |
<U> |
public static Try<U> |
successful(U x)Factory method for success. |
|
public abstract Optional<T> |
toOptional()Try contents wrapped in Optional. |
If a Try is a Success and the predicate holds true, the Success is passed further. Otherwise (Failure or predicate doesn't hold), pass Failure.
pred - predicate applied to the value held by TryTransform success or pass on failure, taking a Try<U> as the result. Takes an optional type parameter of the new type. You need to be specific about the new type if changing type. Try.ofFailable(() -> "1").<Integer>flatMap((x) -> Try.ofFailable(() -> Integer.valueOf(x))) returns Integer(1)
f - function to apply to successful value.U - new type (optional)Gets the value T on Success or throws the cause of the failure.
Gets the value T on Success or throws the cause of the failure wrapped into a RuntimeException
Performs the provided action, when failed
action - action to runPerforms the provided action, when successful
action - action to runReturn a value in the case of a failure. This is similar to recover but does not expose the exception type.
value - return the try's value or else the value specified.Return another try in the case of failure. Like recoverWith but without exposing the exception.
f - return the value or the value from the new try.Try applying f(t) on the case of failure.
f - function that takes throwable and returns resultFactory method for success.
x - value to create the successful Try withU - TypeTry contents wrapped in Optional.