tryOrElse

inline fun <T> tryOrElse(fallback: (Exception) -> T, block: () -> T): T

Runs block and returns its value, or the value of fallback applied to the thrown Exception.

Use this instead of tryOrDefault when the fallback needs the exception, for example to log it or derive a value from it. Only Exception is caught, not Throwable, so Errors and other non-Exception throwables propagate to the caller. This function is not suspending: CancellationException is an Exception and would be swallowed here, so do not wrap suspending calls in it. Suspend-aware variants exist elsewhere in this library as runCatchingCancellable.

Return

the value of block, or the value of fallback when it threw.

Parameters

fallback

recovery that maps the caught Exception to a result value.

block

computation to attempt.