SyntaxError 类


表示程序不是有效的 JavaScript。 这些错误只能作为代码评估的结果生成和传播。 代码评估可能是 evalFunctionrequirevm 的结果。 这些错误几乎始终表明程序损坏。

try {
  require('vm').runInThisContext('binary ! isNotOk');
} catch (err) {
  // 'err' 将是 SyntaxError。
}

SyntaxError 实例在创建它们的上下文中是不可恢复的,它们只能被其他上下文捕获。

Indicates that a program is not valid JavaScript. These errors may only be generated and propagated as a result of code evaluation. Code evaluation may happen as a result of eval, Function, require, or vm. These errors are almost always indicative of a broken program.

try {
  require('vm').runInThisContext('binary ! isNotOk');
} catch (err) {
  // 'err' will be a SyntaxError.
}

SyntaxError instances are unrecoverable in the context that created them – they may only be caught by other contexts.