I am facing one issue regarding coroutines. I am u...
# coroutines
s
I am facing one issue regarding coroutines. I am using
CoroutineExceptionHandler
to catch exception that is written inside a function. While calling that function again it is actually throwing previous Exception without executing any coroutines inside that. Can anybody help me with this.
m
Could you provide a code example, so we can reproduce it?
s
@marstran
Copy code
fun <T, U> CoroutineScope.merge(
    fun1: suspend () -> T,
    fun2: suspend () -> U,
    doOnSuccess: suspend (CoroutineContext, T, U) -> Unit,
    doOnError: () -> Unit,
    doOnNoInternet: (() -> Unit)? = null,
    doOnBefore: (() -> Unit)? = null
) {
    val exceptionHandler: CoroutineContext = CoroutineExceptionHandler { _, throwable ->
        doOnBefore?.invoke()
        if (throwable is NoInternetException)
            doOnNoInternet?.invoke()
        else
            doOnError()
    }
        val job1 = async(<http://Dispatchers.IO|Dispatchers.IO> + exceptionHandler) { fun1.invoke() }
        val job2 = async(<http://Dispatchers.IO|Dispatchers.IO> + exceptionHandler) { fun2.invoke() }
        launch(<http://Dispatchers.IO|Dispatchers.IO> + exceptionHandler) {
            val result1 = job1.await()
            val result2 = job2.await()
            withContext(Dispatchers.Main + exceptionHandler) {
                doOnBefore?.invoke()
                doOnSuccess(exceptionHandler, result1, result2)
            }
        }
}
Copy code
this.job = Job()
    this.coroutineContext = job + Dispatchers.Default
So if I do the above one after getting exception, it’s actually working fine
@gildor could you help me with this.