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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.77.63