runCatching
Capture exceptions as values.
Capturing Exceptions
runCatching runs a block and wraps its outcome in a Result: the return value becomes a success, any thrown exception becomes a failure. No try/catch needed.
Basic Usage
Pass a lambda; the last expression is the success value.
fun main() {
val r = runCatching { 10 / 2 }
println(r.isSuccess)
println(r.getOrNull())
}