- 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.
推薦閱讀
- Learn ECMAScript(Second Edition)
- LabVIEW程序設計基礎與應用
- The Android Game Developer's Handbook
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- AIRAndroid應用開發實戰
- 數據結構與算法JavaScript描述
- Django Design Patterns and Best Practices
- Learning Linux Binary Analysis
- 深入淺出Serverless:技術原理與應用實踐
- C語言程序設計實驗指導 (第2版)
- Haskell Data Analysis Cookbook
- Java Web開發詳解
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- JBoss:Developer's Guide
- ASP.NET 4.0 Web程序設計