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

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.
主站蜘蛛池模板: 彝良县| 化德县| 义马市| 酒泉市| 阳西县| 桂林市| 宁陵县| 行唐县| 桃园市| 休宁县| 明水县| 富蕴县| 青田县| 岱山县| 禄丰县| 阿巴嘎旗| 洛隆县| 城口县| 容城县| 孝昌县| 屏东市| 广丰县| 横山县| 佳木斯市| 新巴尔虎右旗| 铁力市| 资中县| 平塘县| 青河县| 乌拉特中旗| 抚州市| 凉城县| 永春县| 乐平市| 漠河县| 长海县| 赣榆县| 台州市| 克什克腾旗| 吴忠市| 苍溪县|