SyntaxError 类


表明程序不是有效的 JavaScript。 这些错误可能仅在代码评估的结果中产生和传播。 代码评估可能产生自 evalFunctionrequirevm。 这些错误几乎总是表明程序已损坏。

try {
  require('vm').runInThisContext('nodejs.cn ! 中文网');
} 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.