Why Kotlin Coroutine Global scope with Main thread gives exception?

312 views Asked by At

Here is the code, i try to run but it gives me exception.

exception generates only in Dispatchers.Main scope, if i try to use io or default it works properly, no exceptions.

fun main(args: Array<String>) {
        
        println(" Program Starts to run")
        
        GlobalScope.launch(Dispatchers.Main) {
            delay(2000)
            println("Program inside global scope launch")
        }
    
        println(" Program Ends Now")
    }


Exception is::

Exception in thread "main" java.lang.RuntimeException: Exception while trying to handle coroutine exception at kotlinx.coroutines.CoroutineExceptionHandlerKt.handlerException(CoroutineExceptionHandler.kt:38)

I tried to search out this in Google but not find particular answer on it.

0

There are 0 answers