Interface Try<T>

Type Parameters:
T - type of Try's success result.
All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
Try.Failure, Try.Success

public interface Try<T> extends AutoCloseable
Interface to handle exceptions in a functional way.

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.

  • Method Details

    • onSuccess

      default Try<T> onSuccess(Try.CheckedConsumer<? super T> consumer)
      Executes action on success, has no action for failure.
      Parameters:
      consumer - consumer
      Returns:
      this Try or new failure is consumer throws an exception
    • onFailure

      default Try<T> onFailure(Consumer<Exception> consumer)
      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

      default Try<T> recover(Try.CheckedSupplier<? extends T> supplier)
      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

      default Try<T> recover(Try.CheckedSupplier<? extends T> supplier, Predicate<Exception> predicate)
      Tries recover failed try with given supplier, has no action for success.
      Parameters:
      supplier - supplier to recover
      predicate - 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

      default <R> Try<R> map(Try.CheckedFunction<? super T,? extends R> mapper)
      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

      default <R> Try<R> flatMap(Function<? super T,Try<R>> mapper)
      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

      default Try<T> filter(Predicate<? super T> predicate)
      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

      default Stream<T> stream()
    • optional

      default Optional<T> optional()
    • close

      void close()
      Specified by:
      close in interface AutoCloseable
    • 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

      <R> R fold(Function<? super T,? extends R> onSuccess, Function<? super Exception,? extends R> onFailure)
    • isFailure

      default boolean isFailure()
      Checks if it is failure try.
      Returns:
      true if failure.
    • get

      default T get()
      Synonym for orElseThrow()
      Returns:
      see orElseThrow()
    • andFinally

      default Try<T> andFinally(Try.CheckedRunnable runnable)
      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

      default Try<T> peek(Try.CheckedConsumer<Try<T>> consumer)
      Gives access to current Try.
      Parameters:
      consumer - Try's consumer
      Returns:
      this Try or failure if consumer throws an exception.
    • success

      static <T> Try<T> success(T value)
      Factory method to produce Try from value of T type.
      Type Parameters:
      T - result type
      Parameters:
      value - success value
      Returns:
      Try <T>
    • failure

      static <T> Try<T> failure(Exception exception)
      Factory method to produce Try from value of Exception.
      Type Parameters:
      T - result type
      Parameters:
      exception - exception
      Returns:
      Try <T>
    • of

      static <T> Try<T> of(Try.CheckedSupplier<? extends T> supplier)
      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

      static Try<Void> of(Try.CheckedRunnable runnable)
      Factory method to produce Try from runnable that may throw an exception.
      Parameters:
      runnable - exceptional runnable
      Returns:
      Try of Void type
    • catching

      static <T, R> Function<T,Try<R>> catching(Try.CheckedFunction<T,R> func)
      Higher order function to transform partial T->R function to the total T->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 type
      R - function result type
      Parameters:
      func - partial function T->R
      Returns:
      total function T->Try<R>
    • consumeCatching

      static <T> Function<T,Try<T>> consumeCatching(Try.CheckedConsumer<T> cons)
    • getCatching

      static <T> Function<T,Try<T>> getCatching(Try.CheckedSupplier<T> supl)
    • runCatching

      static <T> Function<T,Try<Void>> runCatching(Try.CheckedRunnable run)
    • toFunction

      static <T> Try.CheckedFunction<T,T> toFunction(Try.CheckedConsumer<? super T> cons)
    • toFunction

      static <U> Try.CheckedFunction<Object,U> toFunction(Try.CheckedSupplier<? extends U> supplier)
      Converts supplier to function.