result

inline fun <T> result(finally: () -> Unit = ::unit, block: () -> T): KotlinResult<T>

Non-suspend runner that catches and encapsulates T and all thrown Exceptions as a KotlinResult.

  • Will catch CancellationException. Use resultOf in suspend contexts to avoid swallowing cancellation.

  • Does NOT catch Error subtypes. Errors are always fatal.

val a: KotlinResult<String> = result { throw Exception() } // Result.failure(Exception())
val b: KotlinResult<String> = result { "Hello, World!" } // Result.success("Hello, World!")

Return

The KotlinResult of block.

Parameters

T

The return type of block.

finally

An optional cleanup block, always runs after block.

block

The protected block to execute. May throw an exception.

See also

Throws

Always propagated — errors are fatal.