JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

11.6 Multiple Catch Clauses

11.6 Multiple Catch Clauses


In JavaScript 1.5, the try/catch statement has been extended to allow multiple catch clauses. To use this feature, follow the name of the catch clause parameter with the if keyword and a conditional expression:

try {

// multiple exception types can be thrown here

throw 1;

}

catch(e if e instanceof ReferenceError) {

// Handle reference errors here

}

catch(e if e === "quit") {

// Handle the thrown string "quit"

}

catch(e if typeof e === "string") {

// Handle any other thrown strings here

}

catch(e) {

// Handle anything else here

}

finally {

// The finally clause works as normal

}

When an exception occurs, each catch clause is tried in turn. The exception is assigned to the named catch clause parameter, and the conditional is evaluated. If true, the body of that catch clause is evaluated, and all other catch clauses are skipped. If a catch clause has no conditional, it behaves as if it has the conditional if true, and it is always triggered if no clause before it was triggered. If all catch clauses have a conditional, and none of those conditionals are true, the exception propagates uncaught. Notice that since the conditionals already appear within the parentheses of the catch clause, they are not required to be directly enclosed in parentheses as they would be in a regular if statement.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27691.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1