public class Try<T>
extends java.lang.Object
implements java.io.Serializable
A try can either be a success of the associated type, or a failure containing an error message.
Try 's allow to chain expressions and propagate errors in a safe and hard-typed manner, rather than throwing exceptions. They furthermore force the user to handle the exception explicitly rather than to rely on chaining throws statements.
Modifier and Type | Method and Description |
---|---|
static <T> Try<T> |
failure(java.lang.String e)
Constructs a failure with the corresponding error message.
|
T |
get()
Returns the result of the corresponding computation if and only if it succeeded.
|
java.lang.String |
getMessage()
Returns the error message if the computation failed.
|
boolean |
isError()
Returns true if the associated computation resulted in an error.
|
boolean |
isSuccess()
Returns true if the associated computation successfully completed.
|
T |
orElse(T t)
Returns the result of the computation if it was a success, or the provided value instead.
|
static <T> Try<T> |
success(T t)
Constructs a success with the corresponding given value.
|
public boolean isError()
public boolean isSuccess()
public T get()
public java.lang.String getMessage()
public T orElse(T t)
public static <T> Try<T> success(T t)
public static <T> Try<T> failure(java.lang.String e)