使用SpiderMonkey,您可以利用条件捕获块将异常路由到适当的处理程序。
try {
// function could throw three exceptions
getCustInfo("Lee", 1234, "lee@netscape.com")
}
catch (e if e == "InvalidNameException") {
// call handler for invalid names
bad_name_handler(e)
}
catch (e if e == "InvalidIdException") {
// call handler for invalid ids
bad_id_handler(e)
}
catch (e if e == "InvalidEmailException") {
// call handler for invalid email addresses
bad_email_handler(e)
}
catch (e){
// don't know what to do, but log it
logError(e)
}
< p >< em >来自MDN的示例
< p>然而,在V8中,这段代码无法编译。除了显而易见的解决方法之外,还有其他建议或解决方法吗?