Skip to content
Commit b91328ab authored by Marcello Galhardo's avatar Marcello Galhardo
Browse files

Add `suspendRunCatching` to safely handle exceptions without a coroutine

Handling any `CancellationException` by mistake inside a Coroutine may break structured concurrency, `suspendRunCatching` provides a easy way to handle exceptions without interfering in structured concurrency.

For example:
```kotlin
suspend fun example(context: Context) {
    val info = suspendRunCatching { context.getSystemService<SystemUpdateManager>()!!.retrieveSystemUpdateInfo() }
            .getOrNull() ?: return // a null return is expected, system update info may not be available.

    // Do something with the info, like trigger an update.
}
```

Test: atest SystemUITests:CoroutineResultTest

Fixes: b/267486825

Change-Id: I5a98913d26ea2a9e447a16f9da494ae18c4f92f1
parent 02cdf72a
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment