https://kotlinlang.org logo
#coroutines
Title
# coroutines
e

elizarov

09/14/2017, 7:48 AM
Let me offer this conceptual model:
launch
is just like starting a new thread. If the code inside
launch
crashes with exception then it is considered “uncaught” exception and, generally, should crash your app (on Android that happens automatically). However, when you use
async
and the execution results with exception, then this exception gets stored inside the
Deferred
. It is your obligation to process it later. So: -
launch
- fire and forget, crash application on uncaught error; -
async
- launch and
await
for result later. You must
await
on a coroutine that you’ve created with
async
. You MUST NOT forget about the coroutine you’ve started with async.
❤️ 1
👍 11
d

deviant

09/14/2017, 8:17 AM
or we can catch exception in
invokeOnCompletion()
after
launch
is finished. or it's not recommended?
e

elizarov

09/14/2017, 12:33 PM
You cannot “catch” it with invokeOnCompletion. You can only get notified on it.
If you want to prevent crash due to uncaught exception, you should install
CoroutineExceptionHandler
in your context.
d

deviant

09/14/2017, 12:34 PM
do you have some examples? din't find it in the doc
d

deviant

09/14/2017, 12:36 PM
so we use it like
launch(UI+myExHandler)
?
e

elizarov

09/14/2017, 12:37 PM
yes
👍 1
d

deviant

09/14/2017, 12:38 PM
thanks! i'll try it
2 Views