可選的 catch 繫結

發布於 · 標籤為 ECMAScript ES2019

try 陳述式的 catch 子句過去需要繫結

try {
doSomethingThatMightThrow();
} catch (exception) {
// ^^^^^^^^^
// We must name the binding, even if we don’t use it!
handleException();
}

在 ES2019 中,catch 現在可以 在沒有繫結的情況下使用。如果你不需要處理例外狀況的程式碼中的 exception 物件,這會很有用。

try {
doSomethingThatMightThrow();
} catch { // → No binding!
handleException();
}

可選的 catch 繫結支援 #