How async and await Handle Errors in Kotlin Coroutines
In this article, we will discuss how error handling works in Kotlin Coroutines when we are using async, await(), and awaitAll()
Why Error Handling in async is Different from launch❗
- launch = (fire — and — forget)
It does not return any value
If something fails inside launch, there is no place to store the exception
➡️ So it throws immediately
➡️ Exception cancels the parent scope (fail-fast)
- async = produces a value (Deferred)
It returns a Deferred: it is like a promise/future
It holds: success result OR exception
➡️ Exceptions inside async are captured, not thrown
➡️ They only get thrown when we do await()
Read the full article here:
Top comments (0)