- 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.
推薦閱讀
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- Getting started with Google Guava
- 技術領導力:程序員如何才能帶團隊
- Python王者歸來
- 老“碼”識途
- C語言實驗指導及習題解析
- R語言與網絡輿情處理
- Python Data Science Cookbook
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- Python網絡爬蟲技術與應用
- GitHub入門與實踐
- Learning iOS Security
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Scrapy網絡爬蟲實戰
- Laravel Design Patterns and Best Practices