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.exampleExceptions06 7 8 import kotlinx.coroutines.* 9 import java.io.* 10 11 @OptIn(DelicateCoroutinesApi::class) 12 fun main() = runBlocking { 13 val handler = CoroutineExceptionHandler { _, exception -> 14 println("CoroutineExceptionHandler got $exception") 15 } 16 val job = GlobalScope.launch(handler) { 17 val inner = launch { // all this stack of coroutines will get cancelled 18 launch { 19 launch { 20 throw IOException() // the original exception 21 } 22 } 23 } 24 try { 25 inner.join() 26 } catch (e: CancellationException) { 27 println("Rethrowing CancellationException with original cause") 28 throw e // cancellation exception is rethrown, yet the original IOException gets to the handler 29 } 30 } 31 job.join() 32 } 33