# try-catch-finally

* [JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [control flow](/web/js/grammar/statement/flow.md) ⟩ [jump](/web/js/grammar/statement/flow/jump.md) ⟩ try-catch-finally
* [JS](/web/js.md) ⟩ [error](/web/js/err.md) ⟩ [handling](/web/js/debug/handling.md) ⟩ try-catch-finally&#x20;

{% hint style="success" %}
([statement](/web/js/grammar/statement.md))&#x20;

```javascript
try { ... }            // run code that may throw.
catch (err) { ... }    // error handling (and∨ rethrow)
finally { ... }        // cleanup code
```

{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %}
一但使用 <mark style="color:blue;">`try`</mark>，一定要用 <mark style="color:blue;">`catch(err)`</mark> 去抓「<mark style="color:yellow;">**所有的錯誤**</mark>」，否則可能會<mark style="color:red;">**漏掉**</mark>「<mark style="color:yellow;">自己沒有預期的錯誤</mark>」:exclamation:

```javascript
try {
  noSuchFunc();  // ⛔ ReferenceError (not catched)
} catch(err) { 
  // `console.log(err)` omitted ... (dangerous)❗
  // or, `throw err` omitted ...
}
// the code will continue executing happily❗
```

{% endhint %}
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
if a <mark style="color:purple;">**finally clause**</mark> <mark style="color:yellow;">**issues**</mark> a [return](/web/js/grammar/statement/flow/jump/return.md) <mark style="color:yellow;">**statement**</mark>, the method <mark style="color:yellow;">**returns**</mark>**&#x20;**<mark style="color:green;">**normally**</mark>, even if an exception has been thrown and has not yet been handled.
{% endhint %}

{% hint style="info" %} <mark style="color:yellow;">**bare**</mark>**&#x20;**<mark style="color:purple;">**catch clause**</mark>

in <mark style="color:yellow;">**ES2019**</mark> and later, the [**parentheses**](/web/js/grammar/token/punctuator/parentheses.md) / (<mark style="color:blue;">`error`</mark>) [**identifier**](/web/js/grammar/token/id.md) in the catch clause <mark style="color:green;">**can**</mark>**&#x20;**<mark style="color:yellow;">**be omitted**</mark>.
{% endhint %}
{% endtab %}

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

* [error handling](/web/js/debug/handling.md)
* [throw](/web/js/debug/handling/throw.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩ 5.5.7 try/catch/finally
  {% endtab %}

{% tab title="📘 手冊" %}

* [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/debug/handling/try-catch-finally.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
