You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fun main() {
runBlocking {
val numberAsync = async<Int> { throw Exception() }
val number = try {
numberAsync.await()
} catch (e: Exception) {
42
}
println(number)
}
}
As Exception is caught by catch block, it is expected for the program to print just `42.
However running it gives the following result:
42
Exception in thread "main" java.lang.Exception
at net.package.MainKt$main$1$numberAsync$1.invokeSuspend(main.kt:9)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:84)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at net.package.MainKt.main(main.kt:8)
at net.package.MainKt.main(main.kt)
Which means runBlocking thrown this exception, even though it had been already caught.
I'm reporting this issue as a bug, because for me it seems to be one, however please let me know if this behaviour is an expected one. If this is the case, it would be worth considering to update the documentation of runBlocking function.
The text was updated successfully, but these errors were encountered:
Concurrent operations like async always work in a scope which gets cancelled on any error. To catch errors arising from async operations use try { ... } catch around coroutineScope:
val number = try {
coroutineScope { // limits the scope of concurrency
val numberAsync = async<Int> { throw Exception() }
numberAsync.await()
}
} catch (e: Exception) {
42
}
println(number)
Kotlin version: 1.4.10
Kotlin coroutines version: 1.4.1
Consider the following code:
As
Exception
is caught bycatch
block, it is expected for the program to print just `42.However running it gives the following result:
Which means
runBlocking
thrown this exception, even though it had been already caught.I'm reporting this issue as a bug, because for me it seems to be one, however please let me know if this behaviour is an expected one. If this is the case, it would be worth considering to update the documentation of
runBlocking
function.The text was updated successfully, but these errors were encountered: