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

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.
主站蜘蛛池模板: 色达县| 原阳县| 沅江市| 武强县| 泰宁县| 开封县| 金溪县| 樟树市| 武城县| 仪陇县| 沾益县| 竹溪县| 保定市| 大埔区| 安多县| 乌拉特后旗| 平谷区| 西乡县| 义马市| 柳河县| 张北县| 安宁市| 辰溪县| 萝北县| 藁城市| 东阿县| 伊宁市| 和平区| 徐汇区| 巍山| 天等县| 浮山县| 德州市| 增城市| 晋宁县| 疏勒县| 延边| 宁城县| 瑞金市| 安陆市| 丹巴县|