In the below example code: ```fun main() = runBloc...
# coroutines
k
In the below example code:
Copy code
fun main() = runBlocking {
    val job = launch {
        repeat(1000) { i ->
            println("job: I'm sleeping $i ...")
            delay(500L)
        }
    }
    delay(1300L) // delay a bit
    println("main: I'm tired of waiting!")
    job.cancel() // cancels the job
    job.join() // waits for job's completion 
    println("main: Now I can quit.")
}
How does Kotlin stop execution of
launch
code block?
l
A
CancellationException
is thrown.
k
Thanks Louis 🙂
Is there a way to know that launch is canceled from inside?
l
You can check
isActive
while catching
CancellationException
, but do you really need to do so?
👍 1
k
No, I was just trying to understand how exception flows and how coroutine cancels job.