> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/async/promise/error-handling.md).

# error handling

[JS](/web/js.md) ⟩ [async](/web/js/async.md) ⟩ [Promise](/web/js/async/promise.md) ⟩ 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 [Promise](/web/js/async/promise.md), and the <mark style="color:red;">**error stops propagating**</mark>.
  {% endhint %}
  {% endtab %}

{% tab title="🔴 主題" %}

* [recoverable errors](/web/js/async/promise/error-handling/recoverable-errors.md)
* [finally()](/web/js/async/promise/error-handling/finally.md)
  {% endtab %}

{% tab title="💈範例" %}
⬆️ require： [getJSON()](/web/js/async/promise/custom-promises/getjson.md)

```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="📘 手冊" %}

* [Promise](/web/js/async/promise.md) ⟩ [.catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch)
  {% endtab %}

{% tab title="👥 相關" %}

* [getJSON()](/web/js/async/promise/custom-promises/getjson.md)
* [await](/web/js/async/await.md) ⟩ [error handling](/web/js/async/await/error-handling.md)
  {% endtab %}
  {% endtabs %}
