1 /*
<lambda>null2  * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 // This file was automatically generated from exception-handling.md by Knit tool. Do not edit.
6 package kotlinx.coroutines.guide.exampleSupervision01
7 
8 import kotlinx.coroutines.*
9 
10 fun main() = runBlocking {
11     val supervisor = SupervisorJob()
12     with(CoroutineScope(coroutineContext + supervisor)) {
13         // launch the first child -- its exception is ignored for this example (don't do this in practice!)
14         val firstChild = launch(CoroutineExceptionHandler { _, _ ->  }) {
15             println("The first child is failing")
16             throw AssertionError("The first child is cancelled")
17         }
18         // launch the second child
19         val secondChild = launch {
20             firstChild.join()
21             // Cancellation of the first child is not propagated to the second child
22             println("The first child is cancelled: ${firstChild.isCancelled}, but the second one is still active")
23             try {
24                 delay(Long.MAX_VALUE)
25             } finally {
26                 // But cancellation of the supervisor is propagated
27                 println("The second child is cancelled because the supervisor was cancelled")
28             }
29         }
30         // wait until the first child fails & completes
31         firstChild.join()
32         println("Cancelling the supervisor")
33         supervisor.cancel()
34         secondChild.join()
35     }
36 }
37