tryOrDefault

inline fun <T> tryOrDefault(default: T, block: () -> T): T

Runs block and returns its value, or default when it throws an Exception.

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 default when it threw an Exception.

Parameters

default

value returned when block throws an Exception.

block

computation to attempt.