dimsuz
09/07/2023, 12:08 PMtry {
callSuspendFun1()
} catch (e: Throwable) {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
callSuspendFun2()
}
throw e
}
1. Is there something to watch out for when suspending inside catch
?
2. Is it better to re-throw as above? what if I place throw e
inside withContext
?Sam
09/07/2023, 12:28 PMwithContext
will fail immediately on entry if e
was caused by job cancellation. withContext
can also fail on exit if the parent coroutine has become cancelled while it was running. In that case the result could differ depending on whether the throw
is inside or outside.Sam
09/07/2023, 12:29 PMdimsuz
09/07/2023, 12:30 PMRobert Williams
09/07/2023, 1:34 PMfinally
for this?Sam
09/07/2023, 1:35 PMfinally
will run in all cases, including normal return, whereas catch
will only run if there’s an exceptionRobert Williams
09/07/2023, 1:37 PMRobert Williams
09/07/2023, 1:37 PMdimsuz
09/07/2023, 4:15 PMcatch
) and report an error (rethrow)Robert Williams
09/07/2023, 5:30 PMcatch Throwable
with "network request failed" (even ignoring the UX problems of things appearing and disappearing randomly)Robert Williams
09/07/2023, 5:31 PMRobert Williams
09/07/2023, 5:31 PMRobert Williams
09/07/2023, 5:32 PMdimsuz
09/08/2023, 10:20 AMRobert Williams
09/08/2023, 10:40 AMdimsuz
09/08/2023, 11:11 AMcatch
and got answer to that question. How to properly do an "optimistic insert" is another question which I didn't ask, but brought it up only because you were curious on why a suspending side-effect might be desirable in catch.