You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Uncaught exception in a child, by default, cancels parent. In particular, this applies to children created with launch coroutine builder. Note that async and other future-like coroutine builders do not have uncaught exceptions by definition, since all their exceptions are caught and are encapsulated in their result.
The resulting coroutine has a key difference compared with similar primitives in other languages and frameworks: it cancels the parent job (or outer scope) on failure to enforce structured concurrency paradigm. To change that behaviour, supervising parent (SupervisorJob or supervisorScope) can be used.
So does an exception thrown within async{} cancel the parent or not?
A simple test shows that exceptions thrown in async{}do cancel the parent and are not "caught and encapsulated in their result". The documentation for Job.children is wrong.
Exception in thread "main" java.lang.RuntimeException: oh no
at FileKt$foo$2$1.invokeSuspend (File.kt:10)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:106)
The documentation for
Job.children
says:However the documentation for
async
says:So does an exception thrown within
async{}
cancel the parent or not?A simple test shows that exceptions thrown in
async{}
do cancel the parent and are not "caught and encapsulated in their result". The documentation forJob.children
is wrong.https://pl.kotl.in/XJMI9RswS
The text was updated successfully, but these errors were encountered: