2.使用Javascript中的try catch throw处理异常。 Javascript支持了try catch throw,Javascript中定义的异常: (1)EvalError: An error occurs in the eval() function. (2)RangeError: A number value is greater then or less then the number that can be represented in Javascript(Number.MAX_VALUE and Number.MIN_VAKUE). (3)ReferenceError: An illegal reference is used. (4)SyntaxError: A syntax error occus inside of an eval() function call. All other syntax error are reorted by the browser and cannot be handled with a try...catch statement. (5)TypeError. A variables type is unexpected. 6.URIError. An error ocuurs in the encodeURI() or the decodeURI() function. 如: 复制代码 代码如下: <script type="text/javascript"> function CreateError() { throw new Error("Created error by custom."); } try { //throw a error from a function just want to see the call stack in firefox. CreateError(); } catch(error) { var errorMsg = ("Message: " + error.message + "
"); if(typeof(error.stack)!=undefined) { //FF errorMsg += ("Line Number: " + error.lineNumber + "
"); errorMsg += ("File Name: " + error.fileName + "
"); errorMsg += ("Stack Trace:
" + error.stack + "
"); } else { //IE errorMsg += ("Description: " + error.description + "
"); errorMsg += ("Number: " + error.number + "
"); } alert(errorMsg); } finally { //alert("End try catch.message from finally block."); } </script>