Aldo Wachyudi
08/06/2019, 5:24 PMcancellation exception
part. If I put try-catch on the code.
fun main() = runBlocking {
val parentScope = CoroutineScope(SupervisorJob())
parentScope.launch {
try {
println("#1")
launch {
try {
println("#3")
fetchDataOverNetwork()
println("#4")
} catch (e: Exception) {
println("Child's Exception not printed")
} finally {
println("Child's Finally not printed")
}
}
println("#2")
} catch (e: Exception) {
println("Parent's Exception not printed")
} finally {
println("Parent's Finally is printed")
}
}
delay(1_000)
parentScope.cancel()
delay(1_000)
println("Bye")
}
The output is:
#1
#2
Parent's Finally is printed
#3
Bye
Why is the exception and finally block is not executed? Even though in the documentation it says..
They check for cancellation of coroutine and throw CancellationException when cancelled.Under what condition the
CancellationException
is thrown?
Thread in Slack Conversationmiqbaldc
08/07/2019, 2:51 AMAldo Wachyudi
08/07/2019, 2:53 AMsuspend fun fetchDataOverNetwork() {
val tenSecondInMillis = 10000
val result = URL("<https://httpstat.us/200?sleep=$tenSecondInMillis>").readText()
println("$result")
}