Interface Try<T>

Type Parameters:
T - the class of the value
All Known Implementing Classes:
Try.Failure, Try.Success

public interface Try<T>
A container object that contains either a value or exception.

If a value is present, isSuccess() will return true and get() will return the value. If exception occurred, isFailure() will return true and get() will return first occurred exception.

Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifSuccess(ThrowableConsumer) (execute a block of code if the value is present) or ifFailure(ThrowableConsumer) (execute a block of code if the exception is present).

This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Try may have unpredictable results and should be avoided.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    A container object which contains exception.
    static class 
    A container object which contains value.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Try<T>
    failure(Throwable throwable)
    Returns a Try with the specified exception.
    filter(ThrowablePredicate<? super T> predicate)
    If a value is present, and the value matches the given predicate, return a Try describing the value, otherwise return a failed Try.
    <U> Try<U>
    flatMap(ThrowableFunction<? super T,Try<U>> mapper)
    If a value is present, apply the provided Try-bearing mapping function to it, return that result, otherwise return a failed Try.
    static <T> Try<T>
    fromOptional(Optional<T> optional)
    Returns a Try created from specified Optional.
    get()
    If this is Try.Success, returns the value, if this is Try.Failure, throws the exception stored as unchecked.
    void
    If an exception is present, invoke the specified consumer with the value, otherwise do nothing.
    void
    ifSuccess(ThrowableConsumer<? super T> consumer)
    If a value is present, invoke the specified consumer with the value, otherwise do nothing.
    void
    ifSuccessOrElse(ThrowableConsumer<? super T> valueConsumer, ThrowableConsumer<Throwable> throwableConsumer)
    If a value is present, invoke the specified consumer with the value, otherwise invoke the specified consumer with the exception.
    boolean
     
    boolean
     
    <U> Try<U>
    map(ThrowableFunction<? super T,? extends U> mapper)
    If a value is present, apply the provided mapping function to it, and if the result is non-null, return a Try describing the result.
    static <T> Try<T>
    of(ThrowableSupplier<T> supplier)
    Returns a Try by invoking specified supplier.
    default T
    orElse(Supplier<? extends T> other)
    Return the value if present, otherwise invoke other and return the result of that invocation.
    default T
    orElse(T other)
    Return the value if present, otherwise return other.
    Continue Try composition by providing alternative ways to compute desired value
    static <T> Try<T>
    success(T value)
    Returns a Try with the specified value.
    Convert this Try to Optional without preserving exception information.
  • Method Details

    • of

      static <T> Try<T> of(ThrowableSupplier<T> supplier)
      Returns a Try by invoking specified supplier.
      Type Parameters:
      T - the class of the value
      Parameters:
      supplier - the supplier to retrieve the value
      Returns:
      a Try.Success with the value retrieved successfully or Try.Failure
    • fromOptional

      static <T> Try<T> fromOptional(Optional<T> optional)
      Returns a Try created from specified Optional.
      Type Parameters:
      T - the class of the value
      Parameters:
      optional - the Optional to be present
      Returns:
      a Try.Success with the value if it was present otherwise a Try.Failure with NoSuchElementException
      Throws:
      NullPointerException - if optional is null
    • success

      static <T> Try<T> success(T value)
      Returns a Try with the specified value.
      Type Parameters:
      T - the class of the value
      Parameters:
      value - the value to be present
      Returns:
      a Try.Success with the value
      Throws:
      NullPointerException - if value is null
    • failure

      static <T> Try<T> failure(Throwable throwable)
      Returns a Try with the specified exception.
      Type Parameters:
      T - the class of the value
      Parameters:
      throwable - the value to be present, which must be non-null
      Returns:
      a Try.Failure with the exception
      Throws:
      NullPointerException - if throwable is null
    • get

      T get()
      If this is Try.Success, returns the value, if this is Try.Failure, throws the exception stored as unchecked.
      Returns:
      the value held by this Try.Success
      See Also:
    • isSuccess

      boolean isSuccess()
      Returns:
      true if there is a value present, otherwise false
    • isFailure

      boolean isFailure()
      Returns:
      true if there is an exception present, otherwise false
    • ifSuccess

      void ifSuccess(ThrowableConsumer<? super T> consumer)
      If a value is present, invoke the specified consumer with the value, otherwise do nothing.
      Parameters:
      consumer - block to be executed if a value is present
    • ifFailure

      void ifFailure(ThrowableConsumer<Throwable> consumer)
      If an exception is present, invoke the specified consumer with the value, otherwise do nothing.
      Parameters:
      consumer - block to be executed if an exception is present
    • ifSuccessOrElse

      void ifSuccessOrElse(ThrowableConsumer<? super T> valueConsumer, ThrowableConsumer<Throwable> throwableConsumer)
      If a value is present, invoke the specified consumer with the value, otherwise invoke the specified consumer with the exception.
      Parameters:
      valueConsumer - block to be executed if a value is present
      throwableConsumer - block to be executed if an exception is present
    • filter

      Try<T> filter(ThrowablePredicate<? super T> predicate)
      If a value is present, and the value matches the given predicate, return a Try describing the value, otherwise return a failed Try.
      Parameters:
      predicate - a predicate to apply to the value, if present
      Returns:
      a Try.Success describing the value of this Try if a value is present and the value matches the given predicate, otherwise a Try.Failure
    • map

      <U> Try<U> map(ThrowableFunction<? super T,? extends U> mapper)
      If a value is present, apply the provided mapping function to it, and if the result is non-null, return a Try describing the result. Otherwise, return a failed Try.
      Type Parameters:
      U - The type of the result of the mapping function
      Parameters:
      mapper - a mapping function to apply to the value, if present
      Returns:
      a Try.Success describing the result of applying a mapping function to the value of this Try, if a value is present, otherwise a Try.Failure
    • flatMap

      <U> Try<U> flatMap(ThrowableFunction<? super T,Try<U>> mapper)
      If a value is present, apply the provided Try-bearing mapping function to it, return that result, otherwise return a failed Try. This method is similar to map(ThrowableFunction), but the provided mapper is one whose result is already a Try, and if invoked, flatMap does not wrap it with an additional Try.
      Type Parameters:
      U - The type parameter to the Try returned by
      Parameters:
      mapper - a mapping function to apply to the value, if present the mapping function
      Returns:
      the result of applying a Try-bearing mapping function to the value of this Try, if a value is present, otherwise a Try.Failure
    • toOptional

      Optional<T> toOptional()
      Convert this Try to Optional without preserving exception information.
      Returns:
      new Optional containing value or Optional.empty() if there was an exception
    • orElseTry

      Try<T> orElseTry(ThrowableSupplier<T> supplier)
      Continue Try composition by providing alternative ways to compute desired value
      Parameters:
      supplier - to invoke in case of failure.
      Returns:
      new Try instance
    • orElse

      default T orElse(T other)
      Return the value if present, otherwise return other.
      Parameters:
      other - the value to be returned if there is a failure, may be null
      Returns:
      the value, if present, otherwise other
    • orElse

      default T orElse(Supplier<? extends T> other)
      Return the value if present, otherwise invoke other and return the result of that invocation.
      Parameters:
      other - a Supplier whose result is returned if no value is present
      Returns:
      the value if present otherwise the result of other.get()
      Throws:
      NullPointerException - if value is not present and other is null