tryRecover

inline fun <Ok, ErrorIn, ErrorOut> Outcome<Ok, ErrorIn>.tryRecover(catch: (throwable: Throwable) -> Outcome<Ok, ErrorOut> = ::rethrow, scope: RaiseScope<ErrorOut> = DefaultRaiseScope(), failure: RaiseScope<ErrorOut>.(ErrorIn) -> Ok): Outcome<Ok, ErrorOut>

Transforms a Failure into a Success, wrapping the result with outcome error-catching semantics.

This is the outcome (non-suspend) alternative to recover — it catches exceptions rather than letting them propagate.

outcome { 4 }                              // Success(4)
.tryRecover { Unit } // No change — Success(4)
.andThen { throw FileNotFoundException("test") } // Failure(FileNotFoundException("test"))
.tryRecover { 7 } // Success(7)

Parameters

Ok

The Success value type.

ErrorIn

The incoming Failure error type.

ErrorOut

The outgoing Failure error type after transformation.

catch

Handles exceptions thrown by failure. Defaults to rethrowing via rethrow.

scope

The RaiseScope used to raise typed errors.

failure

The transform applied to the Failure error value.

See also