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