Result
Result<VALUE> is a sealed class returned by synchronous UseCase.execute() calls. It models success and failure without throwing exceptions.
Variants
Success<VALUE>(val value: VALUE)— holds the successful resultError(val error: Throwable)— holds the exception
Destructuring
Result supports destructuring declarations:
val (value, error) = useCase.execute(args)
// value is non-null on success, error is non-null on failure
CancellationException Handling
Most Result extension functions rethrow CancellationException to respect coroutine cancellation. This ensures that cancelling a coroutine scope still works correctly when using Result chains.
Extension Functions
Extracting Values
getOrNull()— returns the value on success,nullon errorgetOrDefault(defaultValue)— returns the value on success,defaultValueon errorgetOrElse { error -> fallback }— returns the value on success, computes a fallback from the errorgetOrThrow { error -> /* optional pre-throw action */ }— returns the value on success, throws the error otherwisegetOrCancel { error -> /* optional action */ }— returns the value on success, throwsCancellationExceptionon error (cancels the coroutine safely)
Transforming
map { value -> newValue }— transforms the success value; exceptions are rethrownmapCatching { value -> newValue }— transforms the success value; exceptions are caught asError
Recovering
recover { error -> fallbackValue }— recovers from an error with a fallback valuerecoverCatching { error -> fallbackValue }— recovers from an error; exceptions in the recovery block are caught
Folding
fold(onSuccess = { }, onError = { })— handles both success and error cases, returning a single value
Utility Function
tryCatch { block } wraps a block in try-catch, returning Success on success or Error on exception:
val result = tryCatch { riskyOperation() }
Chaining Example
fun loadData() = launchWithHandler {
val data = getDataUseCase.execute()
.map { transform(it) }
.recover { fallbackData }
.getOrThrow()
val saved = saveUseCase.execute(data)
.getOrCancel { showError(it) } // cancels coroutine on error
}