- 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.
推薦閱讀
- Java逍遙游記
- Node.js Design Patterns
- C#程序設計實訓指導書
- SOA實踐
- PHP 7底層設計與源碼實現
- CKA/CKAD應試教程:從Docker到Kubernetes完全攻略
- Java Web開發技術教程
- H5頁面設計:Mugeda版(微課版)
- Python Web數據分析可視化:基于Django框架的開發實戰
- 從Power BI到Analysis Services:企業級數據分析實戰
- Python編程入門(第3版)
- 現代JavaScript編程:經典范例與實踐技巧
- 人件集:人性化的軟件開發
- 像程序員一樣使用MySQL
- VBA Automation for Excel 2019 Cookbook