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

obobo

03/11/2019, 11:12 PM
Hello, I've run into a problem throwing an exception from inside
runBlocking
block. I have a test in which I mock an external service integration throwing an exception. Via
runBlocking
I call the suspending function, which in turn creates a new CoroutineScope using
CoroutineScope(coroutineContext + <http://Dispatchers.IO|Dispatchers.IO>)
and creates many deferred calls using
async
to the mocked external service, then uses
awaitAll()
to get their results. Surrounding this code is a try/catch block, which handles the exception. I've stepped in and all of this is working fine. The problem is when the suspending function returns, the surrounding block declares it exited exceptionally and throws an exception from the runBlocking call. Since I'm handling the exception, what's the right way to prevent the context from picking up the exception?
Thinking about it, seems like what I want to do is when creating the scope is to not use the current context, is that right?
s

sitepodmatt

03/12/2019, 2:39 AM
try/catch handle + supervisorscope
you're missing the latter
if you swallow/handle the exception in the supervisorScope then it won't bubble up