# error handling

[JS](../..) ⟩ [asynchronous](..) ⟩ [await]() ⟩ error handling

{% tabs %}
{% tab title="💈範例" %}
{% hint style="success" %}
catch <mark style="color:purple;">**await**</mark> errors by <mark style="color:blue;">**try...catch**</mark>.
{% endhint %}

```javascript
(async () => {
  // catch await errors by try...catch statement.
  try {
    let response = await fetch(url);
  } catch(err) {
    console.log(err);
  }
})();
```

{% hint style="success" %}
catch [functions](../async/functions "mention") errors by [Promise.prototype.catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch).
{% endhint %}

```javascript
(async () => {
    let response = await fetch(url);
})().catch(err => {    // catch a rejected Promise returned by async func
    console.log(err)
});
```
{% endtab %}

{% tab title="📗 參考" %}
* [ ] JS.info ⟩&#x20;
  * [x] [async / await](https://javascript.info/async-await)
  * [ ] [error handling with Promises](https://javascript.info/promise-error-handling)
{% endtab %}

{% tab title="👥 相關" %}
* [promise](../promise "mention") ⟩ [error-handling](../promise/error-handling "mention")
{% endtab %}

{% tab title="📘 手冊" %}
* [Promise.prototype.catch()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch)
* [ unhandledrejection event](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event)
{% endtab %}
{% endtabs %}
