我对异常处理如何与协程一起工作感到非常困惑。
我希望可以有一个挂起函数链,它们可以像同步代码一样在它们之间传递异常。因此,如果说 Retrofit 抛出 IOException,我可以在挂起函数链的开头处理该异常,例如在演示者中向用户显示错误。
我做了这个简单的例子来尝试协程,但是如果我取消注释 throw Exception
在异常运行失败但异常不会使应用程序崩溃后调用代码。
package com.example.myapplication
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val text = findViewById<TextView>(R.id.thing_text)
val button = findViewById<Button>(R.id.thing_button)
var count = 0
button.setOnClickListener {
launch {
count++
// throw Exception("Boom")
val string = delayedStringOfInt(count)
runOnUiThread { text.text = string }
}
}
}
suspend fun delayedStringOfInt(int: Int): String {
delay(1000)
// throw Exception("Boom")
return int.toString()
}
}
我尝试过使用 async
和 CoroutineExceptionHandler
。
最佳答案
当使用 async
时,您应该在某处等待
结果,这样您就不会丢失任何异常。
关于android - Kotlin协程吞下异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47783141/