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

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.
主站蜘蛛池模板: 乌拉特后旗| 扎囊县| 囊谦县| 甘肃省| 论坛| 岐山县| 五家渠市| 白沙| 平舆县| 吉水县| 广宁县| 连云港市| 隆化县| 龙门县| 当雄县| 凤山县| 沙河市| 河源市| 西城区| 湛江市| 和硕县| 正蓝旗| 简阳市| 乌鲁木齐县| 门源| 清流县| 隆回县| 湟源县| 海伦市| 湟中县| 连南| 苏尼特右旗| 樟树市| 修水县| 庆云县| 登封市| 习水县| 阿瓦提县| 伊宁市| 湘阴县| 漳州市|