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

aaverin

08/24/2018, 5:41 AM
I am a little confused. So if I will not wrap
await()
call in try/catch I might never ever know that there was an exception inside? Or will that exception then thrown as usual?
Copy code
fun main(args: Array<String>) = runBlocking {
    val job = launch {
        println("Throwing exception from launch")
        throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler
    }
    job.join()
    println("Joined failed job")
    val deferred = async {
        println("Throwing exception from async")
        throw ArithmeticException() // Nothing is printed, relying on user to call await
    }
    try {
        deferred.await()
        println("Unreached")
    } catch (e: ArithmeticException) {
        println("Caught ArithmeticException")
    }
}
g

gildor

08/24/2018, 5:53 AM
Deferred throw exception only on call of
await()
See this SO answer from Roman: https://stackoverflow.com/a/46226519/420412
This is difference between
launch
and
async
3 Views