tryRecover

inline fun <T> KotlinResult<T>.tryRecover(onFailure: (Throwable) -> T): KotlinResult<T>

Transform exception into value.

This is the resultOf alternative to Result.recoverCatching.

resultOf { 4 } // KotlinResult.success(4)
.tryRecover { Unit } // No Change - KotlinResult.success(4)
.andThen { throw FileNotFoundException("test") } // KotlinResult.failure(FileNotFoundException("test"))
.tryRecover { 7 } // KotlinResult.success(7)

See also