Kotlin 协程异常,聚合异常 多个异常的处理

    @Test
    fun `test exception aggregation`() = runBlocking<Unit> {
        val coroutineExceptionHandler = CoroutineExceptionHandler { _, exception ->
            println("Caught $exception ${exception.suppressed.contentToString()}")
        }
        val job = GlobalScope.launch(coroutineExceptionHandler) {
            launch {
                try {
                    delay(Long.MAX_VALUE)
                } finally {
                    throw ArithmeticException()
                }
            }
            launch {
                try {
                    delay(Long.MAX_VALUE)
                } finally {
                    throw IndexOutOfBoundsException()
                }
            }
            launch {
                delay(100)
                throw IOException()
            }
        }
        job.join()
    }

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/126458336
今日推荐