https://kotlinlang.org logo
Title
k

kirillrakhman

04/20/2017, 7:56 AM
It's an Android app, I'm doing some async network requests and then I'm manipulation the UI. The network requests can throw `IOException`s or `HttpException`s so I need to catch them, otherwise the app will crash.
e

elizarov

04/20/2017, 7:57 AM
Some you can catch just `IOException`s, can't you?
k

kirillrakhman

04/20/2017, 7:58 AM
and
HttpException
, at least (multi catch would be useful)
but I'm also catching other exceptions and submit them to my crash reporting instead of letting the app crash
e

elizarov

04/20/2017, 8:02 AM
And how do you handle your IOExceptions? Like some specific code everywhere, so you’ve encapsulated the way you handle IOExceptions into some common code?
k

kirillrakhman

04/20/2017, 8:05 AM
I have some generic retry logic for the httprequests (I'm using Rx for this) but if it fails after n times, I just swallow it and set the UI to some fail state
@elizarov so
if (e is CancellationException) throw e
is ok in my catch block?
e

elizarov

04/20/2017, 8:07 AM
I see thanks. Yes, it is Ok. You can also have the same line in your crash reporter.
k

kirillrakhman

04/20/2017, 8:10 AM
ok, thanks for help. do you think there is a way, to streamline this?
e

elizarov

04/20/2017, 8:43 AM
I would extract into some some common code. Actually, I’d install a crash reporter as
CoroutineExceptionHandler
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html in my context and make it ignore
CancellationException
, but report everything else
k

kirillrakhman

04/20/2017, 9:54 AM
but where do I put the UI code then?
e

elizarov

04/20/2017, 10:06 AM
Basically, the idea is that instead of doing
launch(UI) { … }
to start a coroutine you do
luanch(myOwnContext) { … }
, where
myOwnContext = UI + myExceptionHandler
👍 1
c

cbruegg

04/20/2017, 11:56 AM
@kirillrakhman How do you deal with this?
k

kirillrakhman

04/20/2017, 11:57 AM
@cbruegg currently I don't
i.e. I have automatic cancelation based on Navi, but I don't check the lifecycle after every suspension point
@elizarov I understand, but I still need to know when the coroutine fails, so I need to extend my exception logger context and provide that?
e

elizarov

04/20/2017, 12:04 PM
It is hard to get a more specific advice without looking at your actual code
c

cbruegg

04/20/2017, 12:04 PM
Are you sure though your jobs are actually canceled even when they're already in the Handler queue? (If you don't know what I mean and have the time, feel free check out the example project I've linked to)