I can’t catch exception thrown inside async corout...
# coroutines
l
I can’t catch exception thrown inside async coroutine. Can anybody helps, thanks ~ I have a complex business logic, which can finally be simplified like this
Copy code
object CoroutineCrashTest {
    private val scope = GlobalScope + Job()

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

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

    private fun testAsync() = scope.async(Dispatchers.Default) {
        throw IllegalStateException("test")
    }
}
When run() is executed, the
IllegalStateException
will go beyond
doAsyncInside
and finally be handled by the Thread.unchaughtExceptionHandler. On Android the app crashes, on JVM the console logs the exception. However if i change the scope declare to
Copy code
val scope = GlobalScope
Everything works fine, just as expected. Which is really spooky.🤣 BTW: my kotlin version is 1.3.11 my coroutine version is 1.1.0