https://kotlinlang.org logo
#coroutines
Title
# coroutines
w

wcaokaze

11/05/2018, 11:59 AM
Copy code
launch {
    try {
        async { throw IOException() }.await()
    } catch (e: IOException) {
    }
}
I get an uncaught IOException. What is wrong?
Copy code
async {
    try {
        async { throw IOException() }.await()
    } catch (e: IOException) {
    }
}
I get no Exception.
j

Jonathan

11/05/2018, 12:21 PM
This is because
async
is a child of
launch
making it fail imediately.
If the parent is an
async
it is the same, but exception is not logged, because you're supposed to call
await
Just favor suspending function and you'll be fine
One need
async
only for parallel decomposition. For other uses-cases just use suspending functions (without using
async
at all)
w

wcaokaze

11/05/2018, 12:41 PM
Oh, nice URL. Thanks!
8 Views