tryOrNull

inline fun <T> tryOrNull(block: () -> T): T?

Runs block and returns its value, or null 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.

val parsed: Int? = tryOrNull { text.toInt() }

Return

the value of block, or null when it threw an Exception.

Parameters

block

computation to attempt.