Node.js不支持條件catch嘛?
发布于 12 年前 作者 byvoid 6892 次浏览 最后一次编辑是 8 年前

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/try…catch 上面的例子:

try {
   myroutine(); // may throw three exceptions
} catch (e if e instanceof TypeError) {
   // statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
   // statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
   // statements to handle EvalError exceptions
} catch (e) {
   // statements to handle any unspecified exceptions
   logMyErrors(e); // pass exception object to error handler
}

無法在Node.js中運行,錯誤是

SyntaxError: Unexpected token if at Module._compile (module.js:437:25) at Object.Module._extensions…js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)

3 回复

you are not a jser.

什麼意思

应该是不支持 e if e instanceof TypeError这样的语法吧

改用 catch(e){ if (e instanceof TypeError){ }else if (e instanceof xxx){

}

} 这样好了

刚才google 了一下,发现有人有同你同样的问题

http://stackoverflow.com/questions/4635476/v8s-equivalence-of-spidermonkeys-catche-if-e

回到顶部