What am i missing here? handler is not invoked ``...
# coroutines
s
What am i missing here? handler is not invoked
Copy code
fun main( args : Array<String> ) {

    val handler = CoroutineExceptionHandler { _, throwable -> println( "Handled $throwable" ) }

    runBlocking(handler) {

        launch {
            println( "Handler is ${coroutineContext[CoroutineExceptionHandler]}" )
            delay( 1000)
            println( "first task" )
            throw UnsupportedOperationException()
        }

    }

}
a
I would write it like that :
Copy code
import kotlinx.coroutines.*

fun main() = runBlocking {

    val handler = CoroutineExceptionHandler { _, throwable -> println( "Handled $throwable" ) }

    val job = GlobalScope.launch(handler) {
        println( "Handler is $handler" )
            delay( 1000)
            println( "first task" )
            throw UnsupportedOperationException()
    }
}
s
yeah, handler seems to work only with GlobalScope.launch
and not with the scope from runBlocking
a
s
so why even support handlers for launch coroutines?
g
Because this is how coroutines work. exception handler works, but you cannot prevent scope from fail, or you need supervisorScope
s
I was looking at the docs CoroutineExceptionHandler is invoked only on exceptions which are not expected to be handled by the user, so registering it in async builder and the like of it has no effect.
I get why it is ignored for async
but based on that documentation, shouldn't the handler be invoked for launch just like GlobalScopelaunch