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

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.
主站蜘蛛池模板: 阿坝县| 海晏县| 珲春市| 金山区| 台北县| 南乐县| 得荣县| 亚东县| 东辽县| 黄冈市| 独山县| 扎鲁特旗| 霍州市| 兴化市| 南城县| 宁南县| 大同县| 德安县| 固始县| 小金县| 旬阳县| 佛坪县| 洛扎县| 灵宝市| 东丰县| 临漳县| 扎兰屯市| 陇南市| 安塞县| 肇庆市| 广昌县| 潞城市| 五指山市| 保靖县| 巍山| 临安市| 乐东| 马山县| 凤阳县| 绿春县| 乌兰察布市|