dead.fish
03/27/2025, 11:28 AMcontinuation.resumeWithException
instead of simply throwing the exception here?
fun main() {
runBlocking {
try {
suspendCoroutine<Unit> { continuation ->
val e = IllegalArgumentException("foo")
continuation.resumeWithException(e)
//throw e
}
}
catch (_: IllegalArgumentException) {
println("handled")
}
}
}
IOW, how is an exception that is thrown from the block
of suspendCoroutine
handled? The implementation is intrinsic and not visible...Youssef Shoaib [MOD]
03/27/2025, 11:37 AMRobert Williams
03/27/2025, 12:59 PMRobert Williams
03/27/2025, 1:01 PMcontinuation.resume
API and helps to ensure no path leads to coroutine unresumeddead.fish
03/27/2025, 4:00 PMfun main() {
runBlocking {
try {
suspendCoroutine { continuation ->
object: Thread() {
override fun run() {
try {
throw IllegalArgumentException()
} catch (throwable: Throwable) {
continuation.resumeWithException(throwable)
}
}
}.start()
}
}
catch (_: IllegalArgumentException) {
println("handled")
}
}
}
My colleague tells me that resumeWithException
is needed here, otherwise the outer coroutine is leaking and I get a potential deadlock. My main issue here is not the resumeWithException
, I understand that I want to redirect some exceptions onto the calling stack, but that all `Throwable`s are redirected, including everything that inherits from Error
(e.g. OutOfMemoryError
). I have a hard time believing this is a good pattern to forward these to the calling stack as well. Opinions?