分类"JS"下的内容

JavaScript中的try...catch和异常处理

aries 发表于 2007-08-01 2929 次浏览
在JavaScript可以使用try...catch来进行异常处理。例如: try {  foo.bar();} catch (e) {  alert(e.name + ": " + e.message);}目前我们可能得到的系统异常主要包含以下6种: EvalError: raised when an error occurs executing code in eval() RangeError: raised when a numeric variable or parameter is outside of its valid range ReferenceError: raised when de-referencing an invalid reference SyntaxError: raised when a syntax error occurs while parsing code in eval() TypeError: raised when a variable or parameter is not a valid type URIError: raised when encodeURI() or decodeURI() are passed invalid parameters 上面的六种异常对象都继承自Error对象。他们都支持以下两种构造方法: