How can I manage Error in coroutines in case of AP...
# coroutines
a
How can I manage Error in coroutines in case of API requesting? Using RxJava, we can easily manage error in onError operator. I want to know is there any operator like RxJava
z
`try`/`catch`
m
for example,
suspend
functions just throw ordinary exceptions, and you catch them using standard Kotlin `try`/`catch` when you call the
suspend
functions
a
I know `try`/`catch` block catch the exception, but I want to know about coroutines operator like onError, doOnError. Is there any support like these ?
i
runCatching { ... }.onFailure { ... }
👍 1
g
Which is not better imo than try/catch
☝️ 4
Instead of operator, why not just use standard if/else and try catch?
j
Agree with you, just use try/catch just because it’s oldschool doesn’t mean you can’t use it. 🙂
a
I want to add a compose for handling all error from one method than using `try`/`catch`. How can I do it? That's why need an operator.
d
Copy code
inline fun handleErrors(block: () -> Unit) = try {
    block()
} catch(ex: MyError) {
    handleError(ex)
}
m
Just keep in mind that catching
Throwable
also catches the
CancellationException
which typically should be propagated as part of the Coroutines cancellation mechanics.
RxJava doesn't
onError
when disposing an active
Single
.
Coroutines kind of force you to catch the specific exceptions.
z
👍 1
m
note that those are for
Flow
z
Ah, given the bolded references to RxJava I assumed we were talking about Flow 😅 My bad.
👍 1
g
I want to add a compose for handling all error from one method than using `try`/`catch`.
Why you cannot do this with try /catch?
a
The presence of the try-catch block imposes overhead when no exception occurs. That's why I want to avoid `try`/`catch`.
g
No, it doesn’t
It’s very-very optimized, it’s a feature of JVM, not even some language abstraction
✔️ 3
Also, how do you think Rxjava (or any other library) catches those errors in operators? %)
✔️ 2
a
@gildor Thanks for your informative reply.
@gildor, As far as I know, there is no significant performance impact on try block but in catch block greater impact becomes.
g
of course, catch creates Exception with stacktrace, which relatevely heavy object
but you cannot avoid it, anyway you have to create exception, you can of course reuse some existing exception, but it will not have any stacktrace, which make it mostly useless
and again, it’s exactly what happening in all operators
But this is the reason why it’s called “Exception”, it shouldn’t happen during regular invocation flow, it should be some exceptional case, this is one of the reasons why it’s not recommended to use exceptions to control invocation flow
👍 1
a
@gildor I got it, thank you so much.
z
Fun fact: As an optimization, some of the exceptions used internally for cancellation in Flow operators disable stack trace collection, which I believe is where almost all of the exception-related overhead comes from. https://github.com/Kotlin/kotlinx.coroutines/blob/65f1eafbb42f43f7d236048ceb2f2ec4f1291362/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt#L9-L13
👍 1