```object CoroutineCrashTest { private val sco...
# coroutines
s
Copy code
object CoroutineCrashTest {
    private val scope = GlobalScope + Job()

    fun run() = scope.launch(Dispatchers.Default) {
        doAwaitInside()
    }

    private suspend fun doAwaitInside() {
        supervisorScope {
          try {
              testAsync().await()
          } catch (e: IllegalStateException) {
              // catched but still crash
          }
        }
    }

    private fun testAsync() = scope.async(Dispatchers.Default) {
        throw IllegalStateException("test")
    }
}
l
tested, but still got the same result. BTW there is a spooky thing that concerns me. Why is it that when I changed scope form
GlobalScope + Job
(the default CoroutineScope() method always add a job) to
GlobalScope
, the crash goes away?
But this works.
Copy code
private suspend fun testAsync() =
        supervisorScope {
            async(Dispatchers.Default) {
                throw IllegalStateException("test")
            }
        }
l
@Landerl Young You just need to catch exceptions outside of a local
coroutineScope { … }
that encloses
async
calls. No need for
supervisorScope
here. Search for "local scope" in this Slack, you'll see examples if you need them, this has already been covered numerous times.
Also, using
async
the way you showed in this thread is pointless and defeats the purpose async, showing you don't need
async
at all.
l
Thanks so much, I’ll search it! BTW, my logic is much more complex than the code above, it’s just for demonstration purpose.😉