say you have, play link: Kotlin Playground: Edit, Run, Share Kotlin Code Online
import kotlinx.coroutines.*
fun main() {
runBlocking {
launch {
launch {
println(f())
}
}
}
}
suspend fun f(): String {
val scope = CoroutineScope(Dispatchers.IO)
val r = scope.async {
error("oh darn it")
"oh boy!"
}
return try {
r.await()
} catch (e: Exception) {
"oh maybe"
}
}
A quote from a blog, which made me question everything
âAttention: The exception is only encapsulated in the Deferred
, if the async
Coroutine is a top-level Coroutine. Otherwise, the exception is immediately propagated up the job hierarchy and handled by a CoroutineExceptionHandler
or passed to the threadâs uncaught exception handler even without calling .await()
on itâ.
Does suspend fun have some additional impact on exceptions and coroutines? The exception above doesnât bypass the try catch. That async and await are not top level. So, has something changed or is the behavior with suspend functions different.
This is just one of many things after reading or listening to this or that about coroutines, which leads to:
Please consider not writing a blog post or YouTube video until you have done coroutines in the real world. Far too many out there fail and there is no good source I have found yet. Recommendations welcome. We desperately need decent docs on it and the official is not sufficient. I know hard work went into it, thank you, but it falls short.