- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 135字
- 2021-08-05 10:46:51
Exception handling
By default, an exception happening inside a job will be propagated to where it was created. This happens even if we aren't waiting for the job to complete. Consider this example:
fun main(args: Array<String>) = runBlocking {
launch {
TODO("Not Implemented!")
}
delay(500)
}
Here, we use delay() to have the app run for enough time so that the exception is thrown. We aren't using join() on purpose, to show that the exception will be propagated regardless of us waiting for the job to complete.
This will propagate the exception to the uncaught exception handler of the current thread. In a JVM application, this means that the exception will be printed on standard error output, as illustrated:

As we move ahead, we will see how to use CoroutineExceptionHandler and invokeOnCompletion() to handle exceptions.
推薦閱讀
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- Google Apps Script for Beginners
- Delphi程序設計基礎:教程、實驗、習題
- Developing Mobile Web ArcGIS Applications
- HBase從入門到實戰
- Linux:Embedded Development
- SQL Server從入門到精通(第3版)
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- Python期貨量化交易實戰
- 精通Spring:Java Web開發與Spring Boot高級功能
- Python應用與實戰
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- Puppet 5 Beginner's Guide(Third Edition)
- Visual Basic語言程序設計上機指導與練習(第3版)
- Neo4j Graph Data Modeling