官术网_书友最值得收藏!

Starting a coroutine with launch

When your objective is to start a coroutine that doesn't return a result, you should use launch(). It's designed for fire-and-forget scenarios where you only want to be notified if the computation fails, and it still provides you with a function to cancel it if needed. Consider this example:

fun main(args: Array<String>) = runBlocking {
val task = launch {
doSomething()
}
task.join()
println("completed")
}

Here, doSomething() throws an exception:

fun doSomething() {
throw UnsupportedOperationException("Can't do")
}

The exception will be printed to the stack as expected, but notice that the execution was not interrupted and the application finished the execution of main():

As we will see in chapter 3, Life Cycle and Error Handling, the default behavior for uncaught exceptions is defined per platform, but can be overwritten.
主站蜘蛛池模板: 卓尼县| 航空| 阿拉善右旗| 视频| 遵义市| 襄樊市| 台北县| 林州市| 马关县| 什邡市| 博兴县| 呈贡县| 本溪市| 攀枝花市| 上蔡县| 大新县| 莎车县| 宕昌县| 阜新市| 怀仁县| 体育| 肃宁县| 买车| 建水县| 息烽县| 贵州省| 成武县| 寿阳县| 班戈县| 叙永县| 开封县| 读书| 宁晋县| 翁源县| 霍山县| 健康| 新建县| 吐鲁番市| 佛山市| 黄骅市| 靖安县|