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

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.
主站蜘蛛池模板: 灵寿县| 瓮安县| 九龙坡区| 灌南县| 上栗县| 乌拉特后旗| 宁德市| 乐昌市| 桓台县| 祁门县| 班玛县| 深泽县| 上杭县| 厦门市| 苏尼特右旗| 山阴县| 梓潼县| 吐鲁番市| 贺州市| 鄯善县| 沛县| 滕州市| 铜山县| 厦门市| 农安县| 揭西县| 汉中市| 色达县| 贺兰县| 江北区| 贵阳市| 襄樊市| 罗山县| 西林县| 五原县| 龙口市| 西安市| 高雄县| 柳河县| 兰考县| 贞丰县|