Ale*_*kov 7 exception kotlin kotlin-coroutines
我在与协程一起玩耍,发现一些非常奇怪的行为。我想使用转换项目中的一些异步请求suspendCoroutine()
。这是显示此问题的代码。
在第一种情况下,当在runBlocking
协程中调用suspend函数时,来自延续的异常进入catch块,然后runBlocking
成功完成。但是在第二种情况下,当创建新的async
协程时,异常会通过catch块并使整个程序崩溃。
package com.example.lib
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
object Test {
fun runSuccessfulCoroutine() {
runBlocking {
try {
Repository.fail()
} catch (ex: Throwable) {
println("Catching ex in runSuccessfulCoroutine(): $ex")
}
}
}
fun runFailingCoroutine() {
runBlocking {
try {
async { Repository.fail() }.await()
} catch (ex: Throwable) {
println("Catching ex in runFailingCoroutine(): $ex")
}
}
}
}
object Repository {
suspend fun fail(): Int = suspendCoroutine { cont ->
cont.resumeWithException(RuntimeException("Exception at ${Thread.currentThread().name}"))
}
}
fun main() {
Test.runSuccessfulCoroutine()
println()
Test.runFailingCoroutine()
println("We will never get here")
}
Run Code Online (Sandbox Code Playgroud)
那就是在控制台上打印的内容:
Catching ex in runSuccessfulCoroutine(): java.lang.RuntimeException: Exception at main
Catching ex in runFailingCoroutine(): java.lang.RuntimeException: Exception at main
Exception in thread "main" java.lang.RuntimeException: Exception at main
at com.example.lib.Repository.fail(MyClass.kt:32)
at com.example.lib.Test$runFailingCoroutine$1$1.invokeSuspend(MyClass.kt:22)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:236)
at kotlinx.coroutines.EventLoopBase.processNextEvent(EventLoop.kt:123)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:69)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:45)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.example.lib.Test.runFailingCoroutine(MyClass.kt:20)
at com.example.lib.MyClassKt.main(MyClass.kt:41)
at com.example.lib.MyClassKt.main(MyClass.kt)
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况的任何想法-是bug,还是我以错误的方式使用协程?
更新:
使用coroutineScope { ... }
可以缓解runFailingCoroutine()
fun runFailingCoroutine() = runBlocking {
try {
coroutineScope { async { fail() }.await() }
} catch (ex: Throwable) {
println("Catching ex in runFailingCoroutine(): $ex")
}
}
Run Code Online (Sandbox Code Playgroud)
你的第二个例子的行为是正确的,这是结构化并发的工作。因为内部async
块抛出异常,这个协程被取消了。由于结构化并发,父作业也被取消。
看这个小例子:
val result = coroutineScope {
async {
throw IllegalStateException()
}
10
}
Run Code Online (Sandbox Code Playgroud)
即使我们从不请求async
结果,该块也永远不会返回值。内部协程被取消,外部作用域也被取消。
如果您不喜欢这种行为,您可以使用supervisorScope
. 在这种情况下,内部协程可能会失败而不会使外部协程失败。
val result = supervisorScope {
async {
throw IllegalStateException()
}
10
}
Run Code Online (Sandbox Code Playgroud)
在您的第一个示例中,您捕获了协程块内的异常,因此,协程正常退出。
有关此主题的讨论,请参见:
简而言之,这种行为是需要的,因为async
与其他语言的目的不同。在 Kotlin 中,您应该谨慎使用它,仅当您必须将任务分解为多个并行运行的子任务时。
每当你只想写
val result = async { work() }.await()
Run Code Online (Sandbox Code Playgroud)
你应该写
val result = withContext(Default) { work() }
Run Code Online (Sandbox Code Playgroud)
这将按照预期的方式行事。此外,只要有机会,您就应该将withContext
调用移动到work()
函数中并使其成为suspend fun
.
归档时间: |
|
查看次数: |
3898 次 |
最近记录: |