@@ -981,6 +981,62 @@ default Try<T> recoverWith(Function<? super Throwable, ? extends Try<? extends T
981981 }
982982 }
983983
984+ /**
985+ * Returns {@code this}, if this is a {@link Try.Success}, otherwise attempts to recover from any failure
986+ * by evaluating the given {@code recoveryAttempt} (via {@link Try#of(CheckedFunction0)}).
987+ *
988+ * <pre>{@code
989+ * // = Success(5)
990+ * Try.of(() -> 5)
991+ * .recoverAllAndTry(() -> 10);
992+ *
993+ * // = Success(10)
994+ * Try.of(() -> 1/0)
995+ * .recoverAllAndTry(() -> 10);
996+ * }</pre>
997+ *
998+ * @param recoveryAttempt A checked function that provides a fallback in case of a failure
999+ * @return a {@code Try} that is either this {@code Success} or a new {@code Try} evaluated from {@code recoveryAttempt}
1000+ * @throws NullPointerException if {@code recoveryAttempt} is null
1001+ */
1002+ default Try <T > recoverAllAndTry (CheckedFunction0 <? extends T > recoveryAttempt ) {
1003+ Objects .requireNonNull (recoveryAttempt , "recoveryAttempt is null" );
1004+ return isFailure () ? of (recoveryAttempt ) : this ;
1005+ }
1006+
1007+ /**
1008+ * Returns {@code this}, if this is a {@link Try.Success}, otherwise attempts to recover from failure when the
1009+ * underlying cause is assignable to the specified {@code exceptionType}, by evaluating the given
1010+ * {@code recoveryAttempt} (via {@link Try#of(CheckedFunction0)}).
1011+ *
1012+ * <pre>{@code
1013+ * // = Success(5)
1014+ * Try.of(() -> 5)
1015+ * .recoverAndTry(ArithmeticException.class, () -> 10);
1016+ *
1017+ * // = Success(10)
1018+ * Try.of(() -> 1/0)
1019+ * .recoverAndTry(ArithmeticException.class, () -> 10);
1020+ *
1021+ * // = Failure(java.lang.ArithmeticException: / by zero)
1022+ * Try.of(() -> 1/0)
1023+ * .recoverAndTry(NullPointerException.class, () -> 10);
1024+ * }</pre>
1025+ *
1026+ * @param <X> The type of the exception that may be recovered
1027+ * @param exceptionType The specific exception type that should trigger the recovery
1028+ * @param recoveryAttempt A checked function that provides a fallback in case of a matching failure
1029+ * @return a {@code Try} that is either this {@code Success}, or a new {@code Try} evaluated from {@code recoveryAttempt}
1030+ * @throws NullPointerException if {@code exceptionType} or {@code recoveryAttempt} is null
1031+ */
1032+ default <X extends Throwable > Try <T > recoverAndTry (Class <X > exceptionType , CheckedFunction0 <? extends T > recoveryAttempt ) {
1033+ Objects .requireNonNull (exceptionType , "exceptionType is null" );
1034+ Objects .requireNonNull (recoveryAttempt , "recoveryAttempt is null" );
1035+ return isFailure () && exceptionType .isAssignableFrom (getCause ().getClass ())
1036+ ? of (recoveryAttempt )
1037+ : this ;
1038+ }
1039+
9841040 /**
9851041 * Converts this {@code Try} to an {@link Either}.
9861042 *
0 commit comments