5

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

IR42
  • 8,587
  • 2
  • 23
  • 34
  • @AnimeshSahu Can you provide any sources describing this behavior? I tried to throw an exception in the coroutine in IDEA and it shows an error message – IR42 Jan 07 '21 at 17:45
  • I was wrong about the `launch` one! They should've been printed if they're launched using `CoroutineScope.launch`, if not there seems to be a CoroutineExceptionHandler in the scope or it might be a bug. – Animesh Sahu Jan 07 '21 at 18:17
  • 1
    the CoroutineExceptionHandler only triggers if attached to GlobalScope, as per [this](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/exception-handling.md#cancellation-and-exceptions) - If a coroutine encounters an exception other than CancellationException, it cancels its parent with that exception. This behaviour cannot be overridden and is used to provide stable coroutines hierarchies for structured concurrency. CoroutineExceptionHandler implementation is not used for child coroutines. – Stachu Jan 07 '21 at 22:43
  • I do not use and do not plan to use CoroutineExceptionHandler, all I need is for logcat to show messages of thrown exceptions in coroutines so that I can understand where this happened and why – IR42 Jan 08 '21 at 08:45
  • are you using device? that is Huawei??? – Sadegh J Jan 08 '21 at 13:02
  • 1
    @Sadegh I am using Xiaomi and it seems that the problem is in the phone, because with the emulator and other phones the stacktrace is displayed in the logcat, thanks for a hint, do you know how to fix this on xiaomi? – IR42 Jan 08 '21 at 13:52
  • I don't know about Xiaomi. but this solution is for Huawei. you must find hidden menu in Xiaomi. take a look maybe help. https://stackoverflow.com/questions/42691076/logcat-not-showing-errors-from-my-huawei-p9-phone – Sadegh J Jan 08 '21 at 14:11

2 Answers2

1

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)
        }
    }
}
Gohan
  • 2,422
  • 2
  • 26
  • 45
0

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()
        }
    }
}
Siddharth Kamaria
  • 2,448
  • 2
  • 17
  • 37