outcome

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

Runs block inside a RaiseScope and returns an Outcome.

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

Warning: Does not propagate kotlinx.coroutines.CancellationException — use outcomeOf in suspend contexts.

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> = outcome(catch = ::Failure) { /* ... */ }

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

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

Parameters

catch

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

block

The block to execute within the scope.

See also