- 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.
推薦閱讀
- Extending Jenkins
- 兩周自制腳本語言
- Practical Internet of Things Security
- HTML5+CSS3基礎開發教程(第2版)
- Essential Angular
- Mastering JavaScript Design Patterns(Second Edition)
- Python Programming for Arduino
- 軟件測試(慕課版)
- Mastering Python
- C語言王者歸來
- Practical Time Series Analysis
- Developing RESTful Web Services with Jersey 2.0
- Flink原理深入與編程實戰:Scala+Java(微課視頻版)
- 中小企業網站建設與管理(靜態篇)
- 算法學習與應用從入門到精通