outcomeOf

inline suspend fun <Ok, Error> outcomeOf(catch: (Exception) -> Outcome<Ok, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), block: suspend RaiseScope<Error>.() -> Ok): Outcome<Ok, Error>

Suspend variant of outcome builder.

Runs block inside a RaiseScope and returns an Outcome. kotlinx.coroutines.CancellationException is correctly propagated for structured concurrency.

A successful return value is wrapped in Success. A raise call short-circuits and wraps the error in Failure.

Note: catch defaults to rethrow. Override it to map exceptions to an Outcome; passing Failure directly forces Error =>Exception, which may conflict with the intended error type.

// Outcome<Unit, Exception> — all exceptions captured
val a: Outcome<Unit, Exception> = outcomeOf(catch = ::Failure) { /* ... */ }

// Outcome<Int, String> — raise to fail
val b: Outcome<Int, String> = outcomeOf {
raise { "error" }
42
}

// Outcome<String, String> — catch inside block
val c: Outcome<String, String> = outcomeOf {
// Catching can internally handle any Throwable, including would-be fatal Errors
catching({ it.message }) { throw OutOfMemoryError() }
}

Parameters

catch

Maps a thrown Exception to an Outcome. Rethrows by default — override to suppress exceptions.

block

The suspend block to execute within the scope.

See also