T
- Value type in the case of success.public abstract class Try<T> extends java.lang.Object implements java.lang.Iterable<T>, Value<T>, java.io.Serializable
The following exceptions are considered to be fatal/non-recoverable:
Important note: Try may re-throw (undeclared) exceptions, e.g. on get()
. From within a
dynamic proxy InvocationHandler
this will lead to an
UndeclaredThrowableException
. For more information, please read
Dynamic Proxy Classes.
Modifier and Type | Class and Description |
---|---|
static class |
Try.Failure<T>
Deprecated.
will be removed from the public API
|
static class |
Try.Success<T>
Deprecated.
will be removed from the public API
|
static class |
Try.WithResources1<T1 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on one AutoCloseable resource. |
static class |
Try.WithResources2<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on two AutoCloseable resources. |
static class |
Try.WithResources3<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on three AutoCloseable resources. |
static class |
Try.WithResources4<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on four AutoCloseable resources. |
static class |
Try.WithResources5<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on five AutoCloseable resources. |
static class |
Try.WithResources6<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on six AutoCloseable resources. |
static class |
Try.WithResources7<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on seven AutoCloseable resources. |
static class |
Try.WithResources8<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable,T8 extends java.lang.AutoCloseable>
A
Try -with-resources builder that operates on eight AutoCloseable resources. |
Modifier and Type | Method and Description |
---|---|
Try<T> |
andFinally(java.lang.Runnable runnable)
Provides try's finally behavior no matter what the result of the operation is.
|
Try<T> |
andFinallyTry(CheckedRunnable runnable)
Provides try's finally behavior no matter what the result of the operation is.
|
Try<T> |
andThen(java.util.function.Consumer<? super T> consumer)
Shortcut for
andThenTry(consumer::accept) , see andThenTry(CheckedConsumer) . |
Try<T> |
andThen(java.lang.Runnable runnable)
Shortcut for
andThenTry(runnable::run) , see andThenTry(CheckedRunnable) . |
Try<T> |
andThenTry(CheckedConsumer<? super T> consumer)
Passes the result to the given
consumer if this is a Success . |
Try<T> |
andThenTry(CheckedRunnable runnable)
Runs the given runnable if this is a
Success , otherwise returns this Failure . |
<R> Try<R> |
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects value that is in the domain of the given
partialFunction by mapping the value to type R . |
Try<java.lang.Throwable> |
failed()
Returns
Success(throwable) if this is a Failure(throwable) , otherwise
a Failure(new NoSuchElementException("Success.failed()")) if this is a Success. |
static <T> Try<T> |
failure(java.lang.Throwable exception)
Creates a
Try.Failure that contains the given exception . |
Try<T> |
filter(java.util.function.Predicate<? super T> predicate)
Shortcut for
filterTry(predicate::test) , see filterTry(CheckedPredicate) }. |
Try<T> |
filter(java.util.function.Predicate<? super T> predicate,
java.util.function.Function<? super T,? extends java.lang.Throwable> errorProvider)
Shortcut for
filterTry(predicate::test, errorProvider::apply) , see
filterTry(CheckedPredicate, CheckedFunction1) }. |
Try<T> |
filter(java.util.function.Predicate<? super T> predicate,
java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
Shortcut for
filterTry(predicate::test, throwableSupplier) , see
filterTry(CheckedPredicate, Supplier) }. |
Try<T> |
filterNot(java.util.function.Predicate<? super T> predicate)
Contrary to filter, see
filter(Predicate) . |
Try<T> |
filterNot(java.util.function.Predicate<? super T> predicate,
java.util.function.Function<? super T,? extends java.lang.Throwable> errorProvider)
Contrary to filter, see
filter(Predicate, Function) . |
Try<T> |
filterNot(java.util.function.Predicate<? super T> predicate,
java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
Contrary to filter, see
filter(Predicate, Supplier) . |
Try<T> |
filterTry(CheckedPredicate<? super T> predicate)
Returns
this if this is a Failure or this is a Success and the value satisfies the predicate. |
Try<T> |
filterTry(CheckedPredicate<? super T> predicate,
CheckedFunction1<? super T,? extends java.lang.Throwable> errorProvider)
Returns
this if this is a Failure or this is a Success and the value satisfies the predicate. |
Try<T> |
filterTry(CheckedPredicate<? super T> predicate,
java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
Returns
this if this is a Failure or this is a Success and the value satisfies the predicate. |
<U> Try<U> |
flatMap(java.util.function.Function<? super T,? extends Try<? extends U>> mapper)
Shortcut for
flatMapTry(mapper::apply) , see flatMapTry(CheckedFunction1) . |
<U> Try<U> |
flatMapTry(CheckedFunction1<? super T,? extends Try<? extends U>> mapper)
FlatMaps the value of a Success or returns a Failure.
|
<X> X |
fold(java.util.function.Function<? super java.lang.Throwable,? extends X> ifFail,
java.util.function.Function<? super T,? extends X> f)
Folds either the
Failure or the Success side of the Try value. |
abstract T |
get()
Gets the result of this Try if this is a
Success or throws if this is a Failure . |
abstract java.lang.Throwable |
getCause()
Gets the cause if this is a Failure or throws if this is a Success.
|
T |
getOrElseGet(java.util.function.Function<? super java.lang.Throwable,? extends T> other) |
<X extends java.lang.Throwable> |
getOrElseThrow(java.util.function.Function<? super java.lang.Throwable,X> exceptionProvider) |
boolean |
isAsync()
A
Try 's value is computed synchronously. |
abstract boolean |
isEmpty()
Checks whether this Try has no result, i.e.
|
abstract boolean |
isFailure()
Checks if this is a Failure.
|
boolean |
isLazy()
A
Try 's value is computed eagerly. |
boolean |
isSingleValued()
A
Try is a single-valued. |
abstract boolean |
isSuccess()
Checks if this is a Success.
|
Iterator<T> |
iterator()
Returns a rich
io.vavr.collection.Iterator . |
<U> Try<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
Shortcut for
mapTry(mapper::apply) , see mapTry(CheckedFunction1) . |
Try<T> |
mapFailure(API.Match.Case<? extends java.lang.Throwable,? extends java.lang.Throwable>... cases)
Maps the cause to a new exception if this is a
Failure or returns this instance if this is a Success . |
<U> Try<U> |
mapTry(CheckedFunction1<? super T,? extends U> mapper)
Runs the given checked function if this is a
Try.Success ,
passing the result of the current expression to it. |
static <T> Try<T> |
narrow(Try<? extends T> t)
Narrows a widened
Try<? extends T> to Try<T>
by performing a type-safe cast. |
static <T> Try<T> |
of(CheckedFunction0<? extends T> supplier)
Creates a Try of a CheckedFunction0.
|
static <T> Try<T> |
ofCallable(java.util.concurrent.Callable<? extends T> callable)
Creates a Try of a Callable.
|
static <T> Try<T> |
ofSupplier(java.util.function.Supplier<? extends T> supplier)
Creates a Try of a Supplier.
|
<X extends java.lang.Throwable> |
onFailure(java.lang.Class<X> exceptionType,
java.util.function.Consumer<? super X> action)
Consumes the cause if this is a
Try.Failure and the cause is instance of X . |
Try<T> |
onFailure(java.util.function.Consumer<? super java.lang.Throwable> action)
Consumes the cause if this is a
Try.Failure . |
Try<T> |
onSuccess(java.util.function.Consumer<? super T> action)
Consumes the value if this is a
Try.Success . |
Try<T> |
orElse(java.util.function.Supplier<? extends Try<? extends T>> supplier) |
Try<T> |
orElse(Try<? extends T> other) |
void |
orElseRun(java.util.function.Consumer<? super java.lang.Throwable> action) |
Try<T> |
peek(java.util.function.Consumer<? super T> action)
Applies the action to the value of a Success or does nothing in the case of a Failure.
|
Try<T> |
peek(java.util.function.Consumer<? super java.lang.Throwable> failureAction,
java.util.function.Consumer<? super T> successAction)
Applies the
successAction to the value if this is a Success or applies the failureAction to the cause of failure. |
<X extends java.lang.Throwable> |
recover(java.lang.Class<X> exceptionType,
java.util.function.Function<? super X,? extends T> f)
Returns
this , if this is a Success or this is a Failure and the cause is not assignable
from cause.getClass() . |
<X extends java.lang.Throwable> |
recover(java.lang.Class<X> exceptionType,
T value)
Returns
this , if this is a Try.Success or this is a Failure and the cause is not assignable
from cause.getClass() . |
Try<T> |
recover(java.util.function.Function<? super java.lang.Throwable,? extends T> f)
Returns
this , if this is a Success , otherwise tries to recover the exception of the failure with f ,
i.e. |
<X extends java.lang.Throwable> |
recoverWith(java.lang.Class<X> exceptionType,
java.util.function.Function<? super X,Try<? extends T>> f)
Returns
this , if this is a Success or this is a Failure and the cause is not assignable
from cause.getClass() . |
<X extends java.lang.Throwable> |
recoverWith(java.lang.Class<X> exceptionType,
Try<? extends T> recovered)
Recovers this
Try with the given recovered , if this is a Try.Failure
and the given exceptionType is assignable to the underlying cause type. |
Try<T> |
recoverWith(java.util.function.Function<? super java.lang.Throwable,? extends Try<? extends T>> f)
Returns
this , if this is a Success, otherwise tries to recover the exception of the failure with f ,
i.e. |
static Try<java.lang.Void> |
run(CheckedRunnable runnable)
Creates a Try of a CheckedRunnable.
|
static Try<java.lang.Void> |
runRunnable(java.lang.Runnable runnable)
Creates a Try of a Runnable.
|
static <T> Try<Seq<T>> |
sequence(java.lang.Iterable<? extends Try<? extends T>> values)
Reduces many
Try s into a single Try by transforming an
Iterable<Try<? extends T>> into a Try<Seq<T>> . |
static <T> Try<T> |
success(T value)
Creates a
Try.Success that contains the given value . |
Either<java.lang.Throwable,T> |
toEither()
Converts this
Try to an Either . |
Validation<java.lang.Throwable,T> |
toValidation()
Converts this
Try to a Validation . |
<U> Validation<U,T> |
toValidation(java.util.function.Function<? super java.lang.Throwable,? extends U> throwableMapper)
Converts this
Try to a Validation , converting the Throwable (if present)
to another object using passed Function . |
<U> U |
transform(java.util.function.Function<? super Try<T>,? extends U> f)
Transforms this
Try . |
static <T,U> Try<Seq<U>> |
traverse(java.lang.Iterable<? extends T> values,
java.util.function.Function<? super T,? extends Try<? extends U>> mapper)
Maps the values of an iterable to a sequence of mapped values into a single
Try by
transforming an Iterable<? extends T> into a Try<Seq<U>> . |
static <T1 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier)
Creates a
Try -with-resources builder that operates on one AutoCloseable resource. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier)
Creates a
Try -with-resources builder that operates on two AutoCloseable resources. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier,
CheckedFunction0<? extends T3> t3Supplier)
Creates a
Try -with-resources builder that operates on three AutoCloseable resources. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier,
CheckedFunction0<? extends T3> t3Supplier,
CheckedFunction0<? extends T4> t4Supplier)
Creates a
Try -with-resources builder that operates on four AutoCloseable resources. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier,
CheckedFunction0<? extends T3> t3Supplier,
CheckedFunction0<? extends T4> t4Supplier,
CheckedFunction0<? extends T5> t5Supplier)
Creates a
Try -with-resources builder that operates on five AutoCloseable resources. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier,
CheckedFunction0<? extends T3> t3Supplier,
CheckedFunction0<? extends T4> t4Supplier,
CheckedFunction0<? extends T5> t5Supplier,
CheckedFunction0<? extends T6> t6Supplier)
Creates a
Try -with-resources builder that operates on six AutoCloseable resources. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier,
CheckedFunction0<? extends T3> t3Supplier,
CheckedFunction0<? extends T4> t4Supplier,
CheckedFunction0<? extends T5> t5Supplier,
CheckedFunction0<? extends T6> t6Supplier,
CheckedFunction0<? extends T7> t7Supplier)
Creates a
Try -with-resources builder that operates on seven AutoCloseable resources. |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable,T8 extends java.lang.AutoCloseable> |
withResources(CheckedFunction0<? extends T1> t1Supplier,
CheckedFunction0<? extends T2> t2Supplier,
CheckedFunction0<? extends T3> t3Supplier,
CheckedFunction0<? extends T4> t4Supplier,
CheckedFunction0<? extends T5> t5Supplier,
CheckedFunction0<? extends T6> t6Supplier,
CheckedFunction0<? extends T7> t7Supplier,
CheckedFunction0<? extends T8> t8Supplier)
Creates a
Try -with-resources builder that operates on eight AutoCloseable resources. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
collect, collect, contains, corresponds, eq, equals, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, hashCode, narrow, out, out, spliterator, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
public static <T> Try<T> of(CheckedFunction0<? extends T> supplier)
// Returns Success(String)
Try.of(() -> Class.forName("java.lang.String"));
// Returns Failure(ClassNotFoundException)
Try.of(() -> Class.forName("not a class"));
T
- Component typesupplier
- A checked supplierSuccess(supplier.apply())
if no exception occurs, otherwise Failure(throwable)
if an
exception occurs calling supplier.apply()
.public static <T> Try<T> ofSupplier(java.util.function.Supplier<? extends T> supplier)
// Returns Success(123)
Try.ofSupplier(() -> Integer.parseInt("123"));
// Returns Failure(NumberFormatException)
Try.ofSupplier(() -> Integer.parseInt("not a number"));
T
- Component typesupplier
- A supplierSuccess(supplier.get())
if no exception occurs, otherwise Failure(throwable)
if an
exception occurs calling supplier.get()
.public static <T> Try<T> ofCallable(java.util.concurrent.Callable<? extends T> callable)
// Returns Success("hello")
Try.ofCallable(() -> "hello");
// Returns Failure(Exception)
Try.ofCallable(() -> {
throw new Exception("An unknown error occurred.");
});
T
- Component typecallable
- A callableSuccess(callable.call())
if no exception occurs, otherwise Failure(throwable)
if an
exception occurs calling callable.call()
.public static Try<java.lang.Void> run(CheckedRunnable runnable)
// Returns Success(null)
Try.run(() -> doSomeAction());
// Returns Failure(Exception)
Try.run(() -> {
throw new Exception("An unknown error occurred.");
});
runnable
- A checked runnableSuccess(null)
if no exception occurs, otherwise Failure(throwable)
if an exception occurs
calling runnable.run()
.public static Try<java.lang.Void> runRunnable(java.lang.Runnable runnable)
// Returns Success(null)
Try.runRunnable(() -> doSomeAction());
// Returns Failure(RuntimeException)
Try.runRunnable(() -> {
throw new RuntimeException("An unknown error occurred.");
});
runnable
- A runnableSuccess(null)
if no exception occurs, otherwise Failure(throwable)
if an exception occurs
calling runnable.run()
.public static <T> Try<Seq<T>> sequence(java.lang.Iterable<? extends Try<? extends T>> values)
Try
s into a single Try
by transforming an
Iterable<Try<? extends T>>
into a Try<Seq<T>>
. If any of
the Try
s are Try.Failure
, then this returns a Try.Failure
.
Try<String> first = Try.success("hello");
Try<String> second = Try.success("world");
Try<String> third = Try.failure(new Exception());
// Returns Success(Vector("hello", "world"))
Try<Seq<String>> success = Try.sequence(Arrays.asList(first, second));
// Returns Failure(Exception)
Try<Seq<String>> failure = Try.sequence(Arrays.asList(first, second, third));
T
- type of the Trysvalues
- An Iterable
of Try
sTry
of a Seq
of resultsjava.lang.NullPointerException
- if values
is nullpublic static <T,U> Try<Seq<U>> traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Try<? extends U>> mapper)
Try
by
transforming an Iterable<? extends T>
into a Try<Seq<U>>
.
Function<String, Try<Integer>> mapper = number -> Try.of(() -> Integer.parseInt(number));
// Returns Success(Vector(123, 456))
Try<Seq<Integer>> success = Try.traverse(Arrays.asList("123", "456"), mapper);
// Returns Failure(NumberFormatException)
Try<Seq<Integer>> failure = Try.traverse(Arrays.asList("not", "a number"), mapper);
T
- The type of the given values.U
- The mapped value type.values
- An Iterable
of values.mapper
- A mapper of values to TrysTry
of a Seq
of results.java.lang.NullPointerException
- if values or f is null.public static <T> Try<T> success(T value)
Try.Success
that contains the given value
.
// Returns Success(123)
Try<Integer> success = Try.success(123);
T
- Type of the given value
.value
- A value.Success
.public static <T> Try<T> failure(java.lang.Throwable exception)
Try.Failure
that contains the given exception
.
// Returns Failure(NumberFormatException)
Try<Integer> failure = Try.failure(new NumberFormatException());
T
- Component type of the Try
.exception
- An exception.Failure
.public static <T> Try<T> narrow(Try<? extends T> t)
Try<? extends T>
to Try<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.
// Returns Success(123)
Try<Integer> number = Try.success(123);
Try<Number> numberNumber = Try.narrow(number);
T
- Component type of the Try
.t
- A Try
.t
instance as narrowed type Try<T>
.public final Try<T> andThen(java.util.function.Consumer<? super T> consumer)
andThenTry(consumer::accept)
, see andThenTry(CheckedConsumer)
.consumer
- A consumerTry
if this is a Failure
or the consumer succeeded, otherwise the
Failure
of the consumption.java.lang.NullPointerException
- if consumer
is nullpublic final Try<T> andThenTry(CheckedConsumer<? super T> consumer)
consumer
if this is a Success
.
The main use case is chaining checked functions using method references:
Try.of(() -> 100)
.andThen(i -> System.out.println(i));
consumer
- A checked consumerTry
if this is a Failure
or the consumer succeeded, otherwise the
Failure
of the consumption.java.lang.NullPointerException
- if consumer
is nullpublic final Try<T> andThen(java.lang.Runnable runnable)
andThenTry(runnable::run)
, see andThenTry(CheckedRunnable)
.runnable
- A runnableTry
if this is a Failure
or the runnable succeeded, otherwise the
Failure
of the run.java.lang.NullPointerException
- if runnable
is nullpublic final Try<T> andThenTry(CheckedRunnable runnable)
Success
, otherwise returns this Failure
.
The main use case is chaining runnables using method references:
Try.run(A::methodRef).andThen(B::methodRef).andThen(C::methodRef);
Please note that these lines are semantically the same:
Try.run(this::doStuff)
.andThen(this::doMoreStuff)
.andThen(this::doEvenMoreStuff);
Try.run(() -> {
doStuff();
doMoreStuff();
doEvenMoreStuff();
});
runnable
- A checked runnableTry
if this is a Failure
or the runnable succeeded, otherwise the
Failure
of the run.java.lang.NullPointerException
- if runnable
is nullpublic final <R> Try<R> collect(PartialFunction<? super T,? extends R> partialFunction)
partialFunction
by mapping the value to type R
.
partialFunction.isDefinedAt(value)
If the element makes it through that filter, the mapped instance is wrapped in Try
R newValue = partialFunction.apply(value)
R
- The new value typepartialFunction
- A function that is not necessarily defined on value of this try.Try
instance containing value of type R
java.lang.NullPointerException
- if partialFunction
is nullpublic final Try<java.lang.Throwable> failed()
Success(throwable)
if this is a Failure(throwable)
, otherwise
a Failure(new NoSuchElementException("Success.failed()"))
if this is a Success.public final Try<T> filter(java.util.function.Predicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
filterTry(predicate::test, throwableSupplier)
, see
filterTry(CheckedPredicate, Supplier)
}.predicate
- A predicatethrowableSupplier
- A supplier of a throwableTry
instancejava.lang.NullPointerException
- if predicate
or throwableSupplier
is nullpublic final Try<T> filter(java.util.function.Predicate<? super T> predicate, java.util.function.Function<? super T,? extends java.lang.Throwable> errorProvider)
filterTry(predicate::test, errorProvider::apply)
, see
filterTry(CheckedPredicate, CheckedFunction1)
}.predicate
- A predicateerrorProvider
- A function that provides some kind of Throwable for TTry
instancejava.lang.NullPointerException
- if predicate
or errorProvider
is nullpublic final Try<T> filter(java.util.function.Predicate<? super T> predicate)
filterTry(predicate::test)
, see filterTry(CheckedPredicate)
}.predicate
- A predicateTry
instancejava.lang.NullPointerException
- if predicate
is nullpublic final Try<T> filterNot(java.util.function.Predicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
filter(Predicate, Supplier)
.predicate
- A predicatethrowableSupplier
- A supplier of a throwableTry
instancejava.lang.NullPointerException
- if predicate
or throwableSupplier
is nullpublic final Try<T> filterNot(java.util.function.Predicate<? super T> predicate, java.util.function.Function<? super T,? extends java.lang.Throwable> errorProvider)
filter(Predicate, Function)
.predicate
- A predicateerrorProvider
- A function that provides some kind of Throwable for TTry
instancejava.lang.NullPointerException
- if predicate
or errorProvider
is nullpublic final Try<T> filterNot(java.util.function.Predicate<? super T> predicate)
filter(Predicate)
.predicate
- A predicateTry
instancejava.lang.NullPointerException
- if predicate
is nullpublic final Try<T> filterTry(CheckedPredicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
this
if this is a Failure or this is a Success and the value satisfies the predicate.
Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception
occurs testing the predicate. The returned Failure wraps a Throwable instance provided by the given
throwableSupplier
.
predicate
- A checked predicatethrowableSupplier
- A supplier of a throwableTry
instancejava.lang.NullPointerException
- if predicate
or throwableSupplier
is nullpublic final Try<T> filterTry(CheckedPredicate<? super T> predicate, CheckedFunction1<? super T,? extends java.lang.Throwable> errorProvider)
this
if this is a Failure or this is a Success and the value satisfies the predicate.
Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception
occurs testing the predicate. The returned Failure wraps a Throwable instance provided by the given
errorProvider
.
predicate
- A checked predicateerrorProvider
- A provider of a throwableTry
instancejava.lang.NullPointerException
- if predicate
or errorProvider
is nullpublic final Try<T> filterTry(CheckedPredicate<? super T> predicate)
this
if this is a Failure or this is a Success and the value satisfies the predicate.
Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception
occurs testing the predicate. The returned Failure wraps a NoSuchElementException
instance.
predicate
- A checked predicateTry
instancejava.lang.NullPointerException
- if predicate
is nullpublic final <U> Try<U> flatMap(java.util.function.Function<? super T,? extends Try<? extends U>> mapper)
flatMapTry(mapper::apply)
, see flatMapTry(CheckedFunction1)
.U
- The new component typemapper
- A mapperTry
java.lang.NullPointerException
- if mapper
is nullpublic final <U> Try<U> flatMapTry(CheckedFunction1<? super T,? extends Try<? extends U>> mapper)
U
- The new component typemapper
- A mapperTry
java.lang.NullPointerException
- if mapper
is nullpublic abstract T get()
Success
or throws if this is a Failure
.
IMPORTANT! If this is a Try.Failure
, the underlying cause
of type Throwable
is thrown.
The thrown exception is exactly the same as the result of getCause()
.
public abstract java.lang.Throwable getCause()
java.lang.UnsupportedOperationException
- if this is a Successpublic final boolean isAsync()
Try
's value is computed synchronously.public abstract boolean isEmpty()
public abstract boolean isFailure()
public final boolean isLazy()
Try
's value is computed eagerly.public final boolean isSingleValued()
Try
is a single-valued.isSingleValued
in interface Value<T>
true
public abstract boolean isSuccess()
public final Iterator<T> iterator()
Value
io.vavr.collection.Iterator
.public final <U> Try<U> map(java.util.function.Function<? super T,? extends U> mapper)
mapTry(mapper::apply)
, see mapTry(CheckedFunction1)
.public final Try<T> mapFailure(API.Match.Case<? extends java.lang.Throwable,? extends java.lang.Throwable>... cases)
Failure
or returns this instance if this is a Success
.
If none of the given cases matches the cause, the same Failure
is returned.
cases
- A not necessarily exhaustive sequence of cases that will be matched against a cause.Try
if this is a Failure
, otherwise this.public final <U> Try<U> mapTry(CheckedFunction1<? super T,? extends U> mapper)
Try.Success
,
passing the result of the current expression to it.
If this expression is a Try.Failure
then it'll return a new
Try.Failure
of type R with the original exception.
The main use case is chaining checked functions using method references:
Try.of(() -> 0)
.map(x -> 1 / x); // division by zero
U
- The new component typemapper
- A checked functionTry
java.lang.NullPointerException
- if mapper
is nullpublic final Try<T> onFailure(java.util.function.Consumer<? super java.lang.Throwable> action)
Try.Failure
.
// (does not print anything)
Try.success(1).onFailure(System.out::println);
// prints "java.lang.Error"
Try.failure(new Error()).onFailure(System.out::println);
action
- An exception consumerjava.lang.NullPointerException
- if action
is nullpublic final <X extends java.lang.Throwable> Try<T> onFailure(java.lang.Class<X> exceptionType, java.util.function.Consumer<? super X> action)
Try.Failure
and the cause is instance of X
.
// (does not print anything)
Try.success(1).onFailure(Error.class, System.out::println);
// prints "Error"
Try.failure(new Error())
.onFailure(RuntimeException.class, x -> System.out.println("Runtime exception"))
.onFailure(Error.class, x -> System.out.println("Error"));
X
- the exception type that should be handledexceptionType
- the exception type that is handledaction
- an excpetion consumerjava.lang.NullPointerException
- if exceptionType
or action
is nullpublic final Try<T> onSuccess(java.util.function.Consumer<? super T> action)
Try.Success
.
// prints "1"
Try.success(1).onSuccess(System.out::println);
// (does not print anything)
Try.failure(new Error()).onSuccess(System.out::println);
action
- A value consumerjava.lang.NullPointerException
- if action
is nullpublic final T getOrElseGet(java.util.function.Function<? super java.lang.Throwable,? extends T> other)
public final void orElseRun(java.util.function.Consumer<? super java.lang.Throwable> action)
public final <X extends java.lang.Throwable> T getOrElseThrow(java.util.function.Function<? super java.lang.Throwable,X> exceptionProvider) throws X extends java.lang.Throwable
X extends java.lang.Throwable
public final <X> X fold(java.util.function.Function<? super java.lang.Throwable,? extends X> ifFail, java.util.function.Function<? super T,? extends X> f)
Failure
or the Success
side of the Try value.X
- type of the folded valueifFail
- maps the left value if this is a Failure
f
- maps the value if this is a Success
public final Try<T> peek(java.util.function.Consumer<? super java.lang.Throwable> failureAction, java.util.function.Consumer<? super T> successAction)
successAction
to the value if this is a Success or applies the failureAction
to the cause of failure.failureAction
- A Consumer for the Failure casesuccessAction
- A Consumer for the Success caseTry
public final Try<T> peek(java.util.function.Consumer<? super T> action)
public final <X extends java.lang.Throwable> Try<T> recover(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,? extends T> f)
this
, if this is a Success
or this is a Failure
and the cause is not assignable
from cause.getClass()
. Otherwise tries to recover the exception of the failure with f
,
i.e. calling Try.of(() -> f.apply((X) getCause())
.
// = Success(13)
Try.of(() -> 27/2).recover(ArithmeticException.class, x -> Integer.MAX_VALUE);
// = Success(2147483647)
Try.of(() -> 1/0)
.recover(Error.class, x -> -1)
.recover(ArithmeticException.class, x -> Integer.MAX_VALUE);
// = Failure(java.lang.ArithmeticException: / by zero)
Try.of(() -> 1/0).recover(Error.class, x -> Integer.MAX_VALUE);
X
- Exception typeexceptionType
- The specific exception type that should be handledf
- A recovery function taking an exception of type X
Try
java.lang.NullPointerException
- if exception
is null or f
is nullpublic final <X extends java.lang.Throwable> Try<T> recoverWith(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,Try<? extends T>> f)
this
, if this is a Success
or this is a Failure
and the cause is not assignable
from cause.getClass()
. Otherwise tries to recover the exception of the failure with f
which returns Try.
If isFailure()
returned by f
function is true
it means that recovery cannot take place due to some circumstances.
// = Success(13)
Try.of(() -> 27/2).recoverWith(ArithmeticException.class, x -> Try.success(Integer.MAX_VALUE));
// = Success(2147483647)
Try.of(() -> 1/0)
.recoverWith(Error.class, x -> Try.success(-1))
.recoverWith(ArithmeticException.class, x -> Try.success(Integer.MAX_VALUE));
// = Failure(java.lang.ArithmeticException: / by zero)
Try.of(() -> 1/0).recoverWith(Error.class, x -> Try.success(Integer.MAX_VALUE));
X
- Exception typeexceptionType
- The specific exception type that should be handledf
- A recovery function taking an exception of type X
and returning Try as a result of recovery.
If Try is isSuccess()
then recovery ends up successfully. Otherwise the function was not able to recover.Try
java.lang.NullPointerException
- if exceptionType
or f
is nullpublic final <X extends java.lang.Throwable> Try<T> recoverWith(java.lang.Class<X> exceptionType, Try<? extends T> recovered)
Try
with the given recovered
, if this is a Try.Failure
and the given exceptionType
is assignable to the underlying cause type.
// = Success(13)
Try.of(() -> 27/2).recoverWith(ArithmeticException.class, Try.success(Integer.MAX_VALUE));
// = Success(2147483647)
Try.of(() -> 1/0)
.recoverWith(Error.class, Try.success(-1))
.recoverWith(ArithmeticException.class, Try.success(Integer.MAX_VALUE));
// = Failure(java.lang.ArithmeticException: / by zero)
Try.of(() -> 1/0).recoverWith(Error.class, Try.success(Integer.MAX_VALUE));
X
- type of the exception that should be recoveredexceptionType
- the exception type that is recoveredrecovered
- the substitute for a matching Failure
recovered
if this is a Try.Failure
and the cause is of type X
, else this
java.lang.NullPointerException
- if exceptionType
or recovered
is nullpublic final <X extends java.lang.Throwable> Try<T> recover(java.lang.Class<X> exceptionType, T value)
this
, if this is a Try.Success
or this is a Failure
and the cause is not assignable
from cause.getClass()
. Otherwise returns a Try.Success
containing the given value
.
// = Success(13)
Try.of(() -> 27/2).recover(ArithmeticException.class, Integer.MAX_VALUE);
// = Success(2147483647)
Try.of(() -> 1/0)
.recover(Error.class, -1);
.recover(ArithmeticException.class, Integer.MAX_VALUE);
// = Failure(java.lang.ArithmeticException: / by zero)
Try.of(() -> 1/0).recover(Error.class, Integer.MAX_VALUE);
X
- Exception typeexceptionType
- The specific exception type that should be handledvalue
- A value that is used in case of a recoveryTry
java.lang.NullPointerException
- if exception
is nullpublic final Try<T> recover(java.util.function.Function<? super java.lang.Throwable,? extends T> f)
this
, if this is a Success
, otherwise tries to recover the exception of the failure with f
,
i.e. calling Try.of(() -> f.apply(throwable))
.
// = Success(13)
Try.of(() -> 27/2).recover(x -> Integer.MAX_VALUE);
// = Success(2147483647)
Try.of(() -> 1/0).recover(x -> Integer.MAX_VALUE);
f
- A recovery function taking a ThrowableTry
java.lang.NullPointerException
- if f
is nullpublic final Try<T> recoverWith(java.util.function.Function<? super java.lang.Throwable,? extends Try<? extends T>> f)
this
, if this is a Success, otherwise tries to recover the exception of the failure with f
,
i.e. calling f.apply(cause.getCause())
. If an error occurs recovering a Failure, then the new Failure is
returned.
// = Success(13)
Try.of(() -> 27/2).recoverWith(x -> Try.success(Integer.MAX_VALUE));
// = Success(2147483647)
Try.of(() -> 1/0).recoverWith(x -> Try.success(Integer.MAX_VALUE));
f
- A recovery function taking a ThrowableTry
java.lang.NullPointerException
- if f
is nullpublic final Either<java.lang.Throwable,T> toEither()
Try
to an Either
.Either
public final Validation<java.lang.Throwable,T> toValidation()
Try
to a Validation
.Validation
public final <U> Validation<U,T> toValidation(java.util.function.Function<? super java.lang.Throwable,? extends U> throwableMapper)
Try
to a Validation
, converting the Throwable (if present)
to another object using passed Function
.
Validation<String, Integer> = Try.of(() -> 1/0).toValidation(Throwable::getMessage));
U
- result type of the throwable mapperthrowableMapper
- A transformation from throwable to desired invalid type of new Validation
Validation
java.lang.NullPointerException
- if the given throwableMapper
is null.public final <U> U transform(java.util.function.Function<? super Try<T>,? extends U> f)
Try
.U
- Type of transformation resultf
- A transformationU
java.lang.NullPointerException
- if f
is nullpublic final Try<T> andFinally(java.lang.Runnable runnable)
runnable
- A runnableTry
.java.lang.NullPointerException
- if runnable
is nullpublic final Try<T> andFinallyTry(CheckedRunnable runnable)
runnable
- A runnableTry
.java.lang.NullPointerException
- if runnable
is nullpublic static <T1 extends java.lang.AutoCloseable> Try.WithResources1<T1> withResources(CheckedFunction0<? extends T1> t1Supplier)
Try
-with-resources builder that operates on one AutoCloseable
resource.T1
- Type of the 1st resource.t1Supplier
- The supplier of the first resource.Try.WithResources1
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable> Try.WithResources2<T1,T2> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier)
Try
-with-resources builder that operates on two AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.Try.WithResources2
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable> Try.WithResources3<T1,T2,T3> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier)
Try
-with-resources builder that operates on three AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.T3
- Type of the 3rd resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.t3Supplier
- The supplier of the 3rd resource.Try.WithResources3
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable> Try.WithResources4<T1,T2,T3,T4> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier)
Try
-with-resources builder that operates on four AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.T3
- Type of the 3rd resource.T4
- Type of the 4th resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.t3Supplier
- The supplier of the 3rd resource.t4Supplier
- The supplier of the 4th resource.Try.WithResources4
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable> Try.WithResources5<T1,T2,T3,T4,T5> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier)
Try
-with-resources builder that operates on five AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.T3
- Type of the 3rd resource.T4
- Type of the 4th resource.T5
- Type of the 5th resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.t3Supplier
- The supplier of the 3rd resource.t4Supplier
- The supplier of the 4th resource.t5Supplier
- The supplier of the 5th resource.Try.WithResources5
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable> Try.WithResources6<T1,T2,T3,T4,T5,T6> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier)
Try
-with-resources builder that operates on six AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.T3
- Type of the 3rd resource.T4
- Type of the 4th resource.T5
- Type of the 5th resource.T6
- Type of the 6th resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.t3Supplier
- The supplier of the 3rd resource.t4Supplier
- The supplier of the 4th resource.t5Supplier
- The supplier of the 5th resource.t6Supplier
- The supplier of the 6th resource.Try.WithResources6
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable> Try.WithResources7<T1,T2,T3,T4,T5,T6,T7> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier, CheckedFunction0<? extends T7> t7Supplier)
Try
-with-resources builder that operates on seven AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.T3
- Type of the 3rd resource.T4
- Type of the 4th resource.T5
- Type of the 5th resource.T6
- Type of the 6th resource.T7
- Type of the 7th resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.t3Supplier
- The supplier of the 3rd resource.t4Supplier
- The supplier of the 4th resource.t5Supplier
- The supplier of the 5th resource.t6Supplier
- The supplier of the 6th resource.t7Supplier
- The supplier of the 7th resource.Try.WithResources7
instance.public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable,T8 extends java.lang.AutoCloseable> Try.WithResources8<T1,T2,T3,T4,T5,T6,T7,T8> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier, CheckedFunction0<? extends T7> t7Supplier, CheckedFunction0<? extends T8> t8Supplier)
Try
-with-resources builder that operates on eight AutoCloseable
resources.T1
- Type of the 1st resource.T2
- Type of the 2nd resource.T3
- Type of the 3rd resource.T4
- Type of the 4th resource.T5
- Type of the 5th resource.T6
- Type of the 6th resource.T7
- Type of the 7th resource.T8
- Type of the 8th resource.t1Supplier
- The supplier of the 1st resource.t2Supplier
- The supplier of the 2nd resource.t3Supplier
- The supplier of the 3rd resource.t4Supplier
- The supplier of the 4th resource.t5Supplier
- The supplier of the 5th resource.t6Supplier
- The supplier of the 6th resource.t7Supplier
- The supplier of the 7th resource.t8Supplier
- The supplier of the 8th resource.Try.WithResources8
instance.