【发布时间】:2022-01-13 23:01:52
【问题描述】:
我无法理解这两个函数之间的区别。为什么 func2 会导致程序崩溃,而 func1 可以捕获异常?
fun main() {
runBlocking {
func1() //Prints exception
func2() //Program crashes
}
}
fun CoroutineScope.func1() {
launch {
try {
throw IllegalArgumentException("error")
} catch (t: Throwable) {
println(t)
}
}
}
fun CoroutineScope.func2() {
try {
launch {
throw IllegalArgumentException("error")
}
} catch (t: Throwable) {
println(t)
}
}
【问题讨论】:
标签: kotlin try-catch kotlin-coroutines