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.exampleExceptions04
7 
8 import kotlinx.coroutines.*
9 
10 fun main() = runBlocking {
11     val handler = CoroutineExceptionHandler { _, exception ->
12         println("CoroutineExceptionHandler got $exception")
13     }
14     val job = GlobalScope.launch(handler) {
15         launch { // the first child
16             try {
17                 delay(Long.MAX_VALUE)
18             } finally {
19                 withContext(NonCancellable) {
20                     println("Children are cancelled, but exception is not handled until all children terminate")
21                     delay(100)
22                     println("The first child finished its non cancellable block")
23                 }
24             }
25         }
26         launch { // the second child
27             delay(10)
28             println("Second child throws an exception")
29             throw ArithmeticException()
30         }
31     }
32     job.join()
33 }
34