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

Evgeniy Zaharov

06/20/2018, 3:19 PM
with changing
launch
to
async
and
join
to
await
everything start to work as expect, I see all three catch (as described in https://github.com/Kotlin/kotlinx.coroutines/issues/61). But with
launch
its very strange that
method-catch
doesn’t call
e

Evgeniy Zaharov

06/20/2018, 4:33 PM
I know that with async/await it’s possible to do it (i have mentioned it in question). The question was about
launch
, about why it’s don’t catched from not suspend method
c

codyoss

06/20/2018, 5:12 PM
when you launch something you have to think about it as it off on another world. So if an exception happens over on Mars you would never know about it on Earth. The await function provides a bridge to join the two worlds so they can interact. That analogy fell apart rather quickly…
v

Vsevolod Tolstopyatov [JB]

06/20/2018, 5:24 PM
launch
is equivalent of running a thread in coroutines world. When you write
Copy code
val thread = thread { throw Exception() }
thread.join()
You don’t expect for
join
to throw, do you? Same about
launch
. But
async
returns
Deferred
, which is analogue of the
Future
in coroutines world. And
future.get
throws an exception if related computation failed with exception
Basically
launch
is fire-and-forget job, while
async
is asynchronous computation which should return its result at some point
e

Evgeniy Zaharov

06/20/2018, 5:52 PM
@codyoss, @Vsevolod Tolstopyatov [JB] thanks for explanation, and especially for the analogy with the thread. Now it’s much clearer.
u

uli

06/20/2018, 11:01 PM
Does a `launch`ed child with an unhandled exception in today's implementation cancel it's parent with that exception, as it used to be the behaviour?
v

Vsevolod Tolstopyatov [JB]

06/22/2018, 9:52 AM
@uli it cancels child with that exception. Though this mechanism is slightly non-deterministic and being reworked under https://github.com/Kotlin/kotlinx.coroutines/issues/333 to provide consistent behaviour for users. As soon as it’s ready I will add guide section about exceptions mental model
👍 1
u

uli

06/22/2018, 1:13 PM
Thanks @Vsevolod Tolstopyatov [JB] for the update
3 Views