Home > Software design >  Unable to catch Exception of suspendCoroutine
Unable to catch Exception of suspendCoroutine

Time:03-05

So,

I have a function that has a callback so I wrapped it up in a suspend function using suspendCoroutine but when it errors out it is crashing the entire app.

For a piece of concept here is what is happening.

val scope = CoroutineScope(Dispatchers.IO)

scope.launch {
    try {
        test()
    } catch (e: Exception) {
         logE { e.asLog() }
    }
}

suspend fun test() = suspendCoroutine<Unit> { cont ->
    someService.doWork(object: callback() {
        fun onFail(e: Exception) {
            cont.resumeWithException(e) // The andoird app is crashing here
        }
    })
}

2022-03-04 16:09:45.410 19289-19438/in.app.android E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-3 Process: in.app.android, PID: 19289 java.lang.Throwable: Umm at in.app.android.MainActivity.test(MainActivity.kt:187) at in.app.android.MainActivity$onCreate$5.invokeSuspend(MainActivity.kt:177) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

How can I catch any exception when getting throws in the suspendCoroutine clause?

CodePudding user response:

As broot mentioned, your log shows that your someService is throwing a Throwable not an Exception. You will be fine if you change your code like this

try {
     test()
} catch (e: Throwable) {
      
}

CodePudding user response:

Did you try suspendCancellableCoroutine ? You can invoke the callback on invokeOnCancellation end of callback method like below :

cont.invokeOnCancellation {
            callback = null
        }

for implementation, make the local variable of your callback and check the CancellableContinuation isActive as following

 fun onFail(e: Exception) {
     if (cont.isActive) {
        cont.resumeWithException(e)
     }
 }
  • Related