gpt4 book ai didi

javascript - 我可以在 Node.js 的 javascript 中使用 catch(e if e instanceof SyntaxError) 吗?

转载 作者:IT老高 更新时间:2023-10-28 23:24:14 29 4
gpt4 key购买 nike

我读到 MDN 上的 try catch(e if e instanceof ...) block ,但是,在 Node.js 中尝试时,我得到一个 SyntaxError: Unexpected token if。

如果这不起作用,是否有另一种方法来捕获特定异常,而不是可能发生的一切?

最佳答案

引用您链接到的 MDN 文档:

Note: This functionality is not part of the ECMAScript specification.

JavaScript 1.5, NES 6.0: Added multiple catch clauses (Netscape extension).

所以:不,这在 Node.js 中是不可能的。当然,您可以使用以下语法作为解决方法:

try {
try_statements
} catch (e) {
if (e instanceof ...)
catch_statements_1
else if (e instanceof ...)
catch_statements_2
else
throw e;
} [finally {
finally_statements
}]

关于javascript - 我可以在 Node.js 的 javascript 中使用 catch(e if e instanceof SyntaxError) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486043/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com