# onError vs try catch vs then catch

**URL:** <https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890>\
**Category:** Client SDKs\
**Tags:** client, web\
**Created:** [June 30, 2022, 5:12pm UTC](https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890 "2022-06-30T17:12:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yasirtrimulabs](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/yasirtrimulabs/32/2622_2.png) [@yasirtrimulabs](https://community.apollographql.com/u/yasirtrimulabs)\
**Post date:** [June 30, 2022, 5:12pm UTC](https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890/1 "2022-06-30T17:12:03Z")

</div>

Hi I am using following mutation:

```auto
const [mutate, {loading}] = useMyMutation({
    onError: (error) => {
      console.log('error', error);
    },
  });

```

And then calling it like

```auto
mutate()
    .then((data) => {
      console.log('ALOO ~ mutate ~ data', data);
    })
    .catch((error) => {
      console.log('ALOO ~ mutate ~ error', error);
    });

```

But if I remove the “catch” from it the “then” code is being run even if there is an error.  
Is this intentional?

---

<div class="post-metadata">

**Author:** ![Dylan\_Wulf](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/dylan_wulf/32/83_2.png) [@Dylan\_Wulf](https://community.apollographql.com/u/Dylan_Wulf)\
**Post date:** [July 2, 2022, 5:56pm UTC](https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890/2 "2022-07-02T17:56:49Z")

</div>

Yeah, I don’t think this is very well documented but if your `useMutation` hook has an `onError` property, then the `mutate` function will never throw an error. If you remove the `onError` from `useMutation` then the `mutate` function can throw errors.

---

<div class="post-metadata">

**Author:** ![yasirtrimulabs](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/yasirtrimulabs/32/2622_2.png) [@yasirtrimulabs](https://community.apollographql.com/u/yasirtrimulabs)\
**Post date:** [July 4, 2022, 11:42am UTC](https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890/3 "2022-07-04T11:42:26Z")

</div>

Yeah I didn’t find any thing on the documentation about this behaviour.  
Do you know any way to change it so that at least the then function don’t run?  
Thanks

---

<div class="post-metadata">

**Author:** ![Dylan\_Wulf](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/dylan_wulf/32/83_2.png) [@Dylan\_Wulf](https://community.apollographql.com/u/Dylan_Wulf)\
**Post date:** [July 5, 2022, 4:04pm UTC](https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890/4 "2022-07-05T16:04:34Z")

</div>

Only way I know of is to remove `onError` from the `useMutation` hook

---

<div class="post-metadata">

**Author:** ![yasirtrimulabs](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/yasirtrimulabs/32/2622_2.png) [@yasirtrimulabs](https://community.apollographql.com/u/yasirtrimulabs)\
**Post date:** [October 7, 2022, 12:55pm UTC](https://community.apollographql.com/t/onerror-vs-try-catch-vs-then-catch/3890/5 "2022-10-07T12:55:51Z")

</div>

Thank you very much 👏 👏 👏 👏
