Exceptions in Coroutines
Exception handling in Coroutines

Exceptions in Coroutines

Hi everyone, today I will be writing about exceptions in Coroutine and how to handle them gracefully.

First let us see some results to understand the exact issue -

Exceptions in Coroutines

Here, the code throws an unhandled exception after printing the "Inside the coroutine" statement. Let us try to handle it using usual try, catch -

Using try catch

The above works as expected since after printing the statement, it comes to the catch block and you can handle it subsequently.

Now let us look at using try and catch outside the launch -

Try catch outside launch

It does not handle the exception as expected and will eventually crash the application -


Try catch not working

Reason for the above - try and catch do not work over here because the exception inside is propagated to the parent level coroutine and it is not able to handle the uncaught exception. So in our application there might be instances where we launch a child coroutine inside a parent coroutine, and if the child coroutine throws exception then the parent coroutine is not able to handle it and it crashes the application. In this case we need to use CoroutineExceptionHandler -

CoroutineExceptionHandler

Now using this, we can catch the exception but what happens when we want to continue with other children coroutines inside the runBlocking(Parent coroutine) even when one child coroutine throws an exception ?

In this case SupervisorScope comes to our rescue, using this we can continue with other children coroutines even when one child coroutine fails. It is useful when you want to go ahead with other apis even when one api fails. Check this -

superVisorScope

I hope you got some insights regarding the exception handling in Coroutines. Do follow me for more such content. Happy coding.





要查看或添加评论,请登录

Siddhant Mishra的更多文章

  • SuspendCoroutine

    SuspendCoroutine

    Today I will be writing about the usage of an important concept in Android namely suspendCoroutine. By definition…

  • findViewById - Internals

    findViewById - Internals

    Hope everyone is doing well. Today I will be writing about the internal working of findViewById in Android.

    2 条评论
  • Lambda functions

    Lambda functions

    Today I will be writing about two different yet intriguing way to write the lambda functions in Kotlin. Conventionally…

    1 条评论
  • Trie data structure

    Trie data structure

    Today I will write about a magical data structure namely Trie. When we talk of storing and retriving some information…

  • Vertical Order traversal in binary tree

    Vertical Order traversal in binary tree

    We generally talk about Inorder, PreOrder, PostOrder and LevelOrder traversals in binary tree, but we generally do not…

  • LCA in Binary Tree

    LCA in Binary Tree

    Today I will be writing about the LCA(Least Common Ancestor) for a binary tree. LCA refers to the node in the binary…

  • Delegates in Kotlin

    Delegates in Kotlin

    Today I will be writing about my understanding regarding the delegates in Kotlin. The english meaning of Delegation…

  • TypeAlias in Kotlin

    TypeAlias in Kotlin

    Today I will be writing about TypeAlias in Kotlin and how do I understand and use it in my day to day coding. Typealias…

  • Reified in Kotlin

    Reified in Kotlin

    Today I will be writing about the type erasure in Java and Kotlin and its solution. The concept of type erasure stems…

    1 条评论
  • Jetpack Compose - Interesting features to dive deeper into.

    Jetpack Compose - Interesting features to dive deeper into.

    Recently I got to migrate my sample project from the conventional XML to Jetpack Compose, and I would like to list down…

    3 条评论

社区洞察

其他会员也浏览了