# error handling

[JS](../..) ⟩ [async](..) ⟩ [Promise]() ⟩ error handling

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}
* [.catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch) **callback** is for：
  * <mark style="color:yellow;">**reporting**</mark> errors
  * <mark style="color:yellow;">**handling**</mark> errors&#x20;
  * <mark style="color:orange;">**recovering**</mark> from errors
{% endhint %}

{% hint style="warning" %}
* [.catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch) **callback** can throw a new error, but if it <mark style="color:yellow;">**returns normally**</mark>, than that <mark style="color:orange;">**return value**</mark> is used to <mark style="color:yellow;">**resolve**</mark> (fulfill) the associated [](<> "mention"), and the <mark style="color:red;">**error stops propagating**</mark>.
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}
* [recoverable-errors](error-handling/recoverable-errors "mention")
* [finally](error-handling/finally "mention")
{% endtab %}

{% tab title="💈範例" %}
⬆️ require： [getjson](custom-promises/getjson "mention")

```javascript
// use second callback parameter to handle errors.
getJSON("/api/user/profile")
    .then(doThisIfFulfilled, doThisIfRejected);

// use .catch() (more idiomatic way)
getJSON("/api/user/profile")
    .then(displayUserProfile)
    .catch(profileError);
```
{% endtab %}

{% tab title="Untitled" %}
* [ ] JS.info ⟩ [error handling with Promises](https://javascript.info/promise-error-handling)
{% endtab %}

{% tab title="📘 手冊" %}
* [](<> "mention") ⟩ [.catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch)
{% endtab %}

{% tab title="👥 相關" %}
* [getjson](custom-promises/getjson "mention")
* [await](../await "mention") ⟩ [error-handling](../await/error-handling "mention")
{% endtab %}
{% endtabs %}
