Let me offer this conceptual model: `launch` is ju...
# coroutines
e
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
or we can catch exception in
invokeOnCompletion()
after
launch
is finished. or it's not recommended?
e
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
do you have some examples? din't find it in the doc
d
so we use it like
launch(UI+myExHandler)
?
e
yes
👍 1
d
thanks! i'll try it