CNode

Node.js不支持條件catch嘛?

Bbyvoid发布于14 年前最后回复14 年前3 回复7883 浏览0 收藏

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)

Y
yansong#1·14 年前

you are not a jser.

B
byvoid#2·14 年前
引用 yansongyou are not a jser.

什麼意思

J
jjx#3·14 年前

应该是不支持 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

参与回复
登录后即可参与回复。登录