You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import kotlinx.coroutines.*
import java.net.UnknownHostException
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers
import java.io.IOException
class RunContext: CoroutineScope{
val scopeJob = Job()
override val coroutineContext: CoroutineContext = Dispatchers.Default + scopeJob + CoroutineExceptionHandler { _, thr -> println("catch exception in handler"); thr.printStackTrace() }
fun launchMain(
block: suspend CoroutineScope.() -> Unit
): Job = launch {
try {
block()
} catch (exc: Exception) {
println("catch exception in launchMain")
exc.printStackTrace()
}
}
fun testExceptionAsync() = launchMain{
println("testExceptionAsync()")
asyncThrow().await()
}
fun asyncThrow(): Deferred<Int> = async {
println("asyncThrow()")
throw IOException()
}
}
fun main(args: Array<String>) {
runBlocking{
println("runBlocking")
RunContext().testExceptionAsync().join()
println("runBlocking done")
}
}
Print out:
runBlocking
testExceptionAsync()
asyncThrow()
catch exception in launchMain
java.io.IOException
at RunContext$asyncThrow$1.invokeSuspend(Simplest version.kt:31)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
catch exception in handler
java.io.IOException
at RunContext$asyncThrow$1.invokeSuspend(Simplest version.kt:31)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
runBlocking done
But if remove scopeJob from coroutineContext overriding:
override val coroutineContext: CoroutineContext = Dispatchers.Default + CoroutineExceptionHandler { _, thr -> println("catch exception in handler"); thr.printStackTrace() }
all works fine:
runBlocking
testExceptionAsync()
asyncThrow()
catch exception in launchMain
java.io.IOException
at RunContext$asyncThrow$1.invokeSuspend(Simplest version.kt:31)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
runBlocking done
The text was updated successfully, but these errors were encountered:
I think it may be related to #893
Print out:
But if remove scopeJob from coroutineContext overriding:
all works fine:
The text was updated successfully, but these errors were encountered: