```object MyExceptionHandler : AbstractCoroutineCo...
# coroutines
u
Copy code
object MyExceptionHandler : AbstractCoroutineContextElement(Key), CoroutineExceptionHandler {

    override fun handleException(context: CoroutineContext, exception: Throwable) {
        println("Exception caught: ${exception} in context ${context}")
    }
}

val myContext = Unconfined + MyExceptionHandler

// Example showing thread and execution order
// Don't use Thread.sleep in production. Just used to not get distracted by delay() suspension
fun main(args: Array<String>) {
    launch(myContext) {
        Thread.sleep(100)
        println("${Thread.currentThread().name}: before future")
        future {
            Thread.sleep(100)
            println("${Thread.currentThread().name}: in future")
        }.await()
        Thread.sleep(100)
        println("${Thread.currentThread().name}: after future")
        throw IllegalStateException()
    }
    println("${Thread.currentThread().name}: returned")
    Thread.sleep(100000)
}