Kotlin GlobalScope协同程序未将错误记录到控制台
为什么下面的代码没有登录到控制台是TimeoutCancellationExceptionKotlin GlobalScope协同程序未将错误记录到控制台,kotlin,kotlin-coroutines,Kotlin,Kotlin Coroutines,为什么下面的代码没有登录到控制台是TimeoutCancellationException @Test fun noExceptionLogged(){ GlobalScope.launch{ withTimeout(4000) { repeat(1000) { i -> println("I'm sleeping $i ...") delay(500L)
@Test fun noExceptionLogged(){
GlobalScope.launch{
withTimeout(4000) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
}
Thread.sleep(20_000)
}
由于
GlobalScope
nature,它似乎是这样工作的。无法取消它,因此它会吞噬取消异常
GlobalScope.launch { // won't print anything
throw CancellationException()
}
GlobalScope.launch { // will print stacktrace
throw RuntimeException()
}
runBlocking { // will print stackrace
throw RuntimeException()
}
GlobalScope.launch { // will print "Hello!"
try {
throw CancellationException()
} catch (e: Exception) {
println("Hello!")
}
}