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