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

mayojava

10/03/2018, 6:59 AM
Copy code
fun main(args: Array<String>) = runBlocking<Unit> {
    try {
        failedConcurrentSum()
    } catch(e: ArithmeticException) {
        println("Computation failed with ArithmeticException")
    }
}

suspend fun failedConcurrentSum(): Int = coroutineScope {
    val one = async<Int> { 
        try {
            delay(Long.MAX_VALUE) // Emulates very long computation
            42
        } finally {
            println("First child was cancelled")
        }
    }
    val two = async<Int> { 
        println("Second child throws an exception")
        throw ArithmeticException()
    }
        one.await() + two.await()
}
it doesnt seem to work for me. The parent coroutine still blocks waiting for the first
async
after the second threw the exception
v

Vsevolod Tolstopyatov [JB]

10/03/2018, 1:18 PM
Have you updated your coroutines version to 0.30.1?
d

Dico

10/03/2018, 2:57 PM
The reason is that the
two
coroutine doesn't throw its exception until you call
two.await()
which doesn't occur until
one.await()
returns.
If the exception thrown by
two
is supposed to cancel the parent before
await()
is called then I guess I don't know.
v

Vsevolod Tolstopyatov [JB]

10/03/2018, 10:03 PM
@Dico this behaviour was changed in the latest realese
1
5 Views