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
Loading
Please register or sign in to comment