- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 134字
- 2021-08-05 10:46:48
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.
推薦閱讀
- LabVIEW Graphical Programming Cookbook
- Python數(shù)據(jù)可視化:基于Bokeh的可視化繪圖
- MariaDB High Performance
- Scientific Computing with Scala
- C# 8.0核心技術(shù)指南(原書(shū)第8版)
- The DevOps 2.5 Toolkit
- Python機(jī)器學(xué)習(xí)之金融風(fēng)險(xiǎn)管理
- Spring Boot+MVC實(shí)戰(zhàn)指南
- R Data Science Essentials
- NGUI for Unity
- 超簡(jiǎn)單:Photoshop+JavaScript+Python智能修圖與圖像自動(dòng)化處理
- Web前端開(kāi)發(fā)技術(shù):HTML、CSS、JavaScript
- PHP項(xiàng)目開(kāi)發(fā)全程實(shí)錄(第4版)
- Mastering OpenStack
- Jakarta EE Cookbook