跳到主要内容

可选的`catch`绑定

· 阅读需 1 分钟
Mathias Bynens ([@mathias](https://twitter.com/mathias))

try语句的catch子句过去需要一个绑定:

try {
doSomethingThatMightThrow();
} catch (exception) {
// ^^^^^^^^^
// 我们必须命名绑定,即使我们不使用它!
handleException();
}

在ES2019中,catch现在可以不带绑定使用。如果您在处理异常的代码中不需要exception对象,这将非常有用。

try {
doSomethingThatMightThrow();
} catch { // → 无绑定!
handleException();
}

可选的catch绑定支持