The catch
clause of try
statements used to require a binding:
try {
doSomethingThatMightThrow();
} catch (exception) {
// ^^^^^^^^^
// We must name the binding, even if we don’t use it!
handleException();
}
In ES2019, catch
can now be used without a binding. This is useful if you don’t have a need for the exception
object in the code that handles the exception.
try {
doSomethingThatMightThrow();
} catch { // → No binding!
handleException();
}
Optional catch
binding support #
- Chrome: 自 66 版本开始支持
- Firefox: 自 58 版本开始支持
- Safari: 支持
- Node.js: 自 8 版本开始支持
- Babel: 支持