'Logcat does not show an error message when an exception is thrown in the coroutine (Xiaomi)

It shows only I/Process: Sending signal. PID: xxxxx SIG: 9

I have tried to enable debug mode

System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)

but it did not help



Solution 1:[1]

I've also faced this issue sometime back. One of the workaround, is to use a CoroutineExceptionHandler. You can create a handler to print your stack trace as follows.

val exceptionHandler = CoroutineExceptionHandler { _, ex ->
    Log.e("CoroutineScope", "Caught ${Log.getStackTraceString(ex)}")
}

And then you can launch your coroutines as someCoroutineScope.launch(exceptionHandler) {}.

Additionally, if you don't want to use handlers in release mode, then you can create your own custom coroutine launcher as an extension function.

fun CoroutineScope.launchCustom(block: suspend CoroutineScope.() -> Unit) : Job {

    return if (BuildConfig.DEBUG) {
        this.launch(exceptionHandler) {
            block()
        }
    } else {
        this.launch {
            block()
        }
    }
}

Solution 2:[2]

I just encounter this problem ... I don't know the reason, very few cases I can found(https://juejin.cn/post/7085643598506491918)

Set an extra DefaultUncaughtExceptionHandler works for me:

object ExceptionHandler {
    fun setupExceptionHandler() {
        val defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
        Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
            val message = "Uncaught exception in thread ${thread.name}:\n"
            Log.e("AndroidRuntime", message, throwable)
            defaultUncaughtExceptionHandler?.uncaughtException(thread, throwable)
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Siddharth Kamaria
Solution 2