https://kotlinlang.org logo
Title
a

Aslam Hossin

08/01/2019, 4:20 PM
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

zak.taccardi

08/01/2019, 4:21 PM
`try`/`catch`
m

Mark Murphy

08/01/2019, 4:23 PM
for example,
suspend
functions just throw ordinary exceptions, and you catch them using standard Kotlin `try`/`catch` when you call the
suspend
functions
a

Aslam Hossin

08/01/2019, 4:24 PM
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

Ilmir Usmanov [JB]

08/01/2019, 4:25 PM
runCatching { ... }.onFailure { ... }
👍 1
g

gildor

08/01/2019, 4:26 PM
Which is not better imo than try/catch
☝️ 4
Instead of operator, why not just use standard if/else and try catch?
j

Joaquim Ley

08/01/2019, 4:28 PM
Agree with you, just use try/catch just because it’s oldschool doesn’t mean you can’t use it. 🙂
a

Aslam Hossin

08/01/2019, 4:31 PM
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

Dico

08/01/2019, 6:14 PM
inline fun handleErrors(block: () -> Unit) = try {
    block()
} catch(ex: MyError) {
    handleError(ex)
}
m

Marcelo Hernandez

08/01/2019, 6:17 PM
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

Zach Klippenstein (he/him) [MOD]

08/01/2019, 6:40 PM
onCompletion
might come in handy too if you need
finally
semantics: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/on-completion.html
👍 1
m

Mark Murphy

08/01/2019, 6:41 PM
note that those are for
Flow
z

Zach Klippenstein (he/him) [MOD]

08/01/2019, 6:42 PM
Ah, given the bolded references to RxJava I assumed we were talking about Flow 😅 My bad.
👍 1
g

gildor

08/02/2019, 12:27 AM
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

Aslam Hossin

08/02/2019, 3:06 AM
The presence of the try-catch block imposes overhead when no exception occurs. That's why I want to avoid `try`/`catch`.
g

gildor

08/02/2019, 3:13 AM
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

Aslam Hossin

08/02/2019, 3:18 AM
@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

gildor

08/02/2019, 3:27 AM
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

Aslam Hossin

08/02/2019, 3:32 AM
@gildor I got it, thank you so much.
z

Zach Klippenstein (he/him) [MOD]

08/02/2019, 6:13 PM
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