js error-tolerant statement, even if js fails, it will not prompt an error (to prevent a yellow triangle symbol in the lower right corner of the browser, or the user experience is not good)
window.onerror=function(){return true;}The following is a method to obtain js exception information and facilitate developers to find the problem:
1. try...catch...
<script type="text/javascript"> var txt="" function message() { try { adddlert("Welcome guest!") } catch(err) { txt="There are errors on this page.\n\n" txt+="Click "OK" to continue viewing this page,\n" txt+="Click "Cancel" to return to the home page.\n\n" if(!confirm(txt)) { document.location.href="/index.html" } } } </script>
2. throw
<script type="text/javascript"> var x=prompt("Please enter a number between 0 and 10: ","") try { if(x>10) throw "Err1" else if(x<0) throw "Err2" else if(isNaN(x)) throw "Err3" } catch (s) { if(er=="Err1") alert("Error! The value is too large!") if(er == "Err2") alert("Error! The value is too small!") if(er == "Err3") alert("Error! The value is not a number!") } </script>
3. onerror
<script type="text/javascript"> onerror=handleErr var txt="" function handleErr(msg,url,l) { txt="There are errors on this page.\n\n" txt+="错误:" + msg + "\n" txt+="URL: " + url + "\n" txt+="行:" + l + "\n\n" txt+="Click OK to continue.\n\n" alert(txt) return true } function message() { adddlert("Welcome guest!") } </script>
Exception handling in js
In JavaScript, try...catch can be used for exception handling. For example:
try { foo.bar();} catch (e) { alert(e.name + ": " + e.message);}
At present, the system exceptions we may get mainly include the following 6 types:
- 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对象。他们都支持以下两种构造方法:
new Error();new Error("异常信息");
手工抛出异常的方法如下:
try { throw new Error("Whoops!");} catch (e) { alert(e.name + ": " + e.message);}
如要判断异常信息的类型,可在catch中进行判断:
try { foo.bar(); } catch (e) { if (e instanceof EvalError) { alert(e.name + ":" + e.message); } else if (e instanceof RangeError) { alert(e.name + ": " + e.message); } // etc }Error具有下面一些主要属性:
- description: 错误描述 (仅IE可用).
- fileName: 出错的文件名 (仅Mozilla可用).
- lineNumber: 出错的行数 (仅Mozilla可用).
- message: 错误信息 (在IE下同description)
- name: 错误类型.
- number: 错误代码 (仅IE可用).
- stack: 像Java中的Stack Trace一样的错误堆栈信息 (仅Mozilla可用).
因此为了更好的了解错误信息我们可以将catch部分改为如下形式:
try { foo.bar(); } catch(e) { if (browserType != BROWSER_IE) { alert("name: " + e.name + "message: " + e.message + "lineNumber: " + e.lineNumber + "fileName: " + e.fileName + "stack: " + e.stack); } else { alert("name: " + e.name + "errorNumber: " + (e.number & 0xFFFF) + "message: " + e.message "); } } "
JavaScript中的throw命令事实上可以抛出任何对象,并且我们可以在catch接受到此对象。例如:
try { throw new Date(); // 抛出当前时间对象 } catch (e) { alert(e.toLocaleString()); // 使用本地格式显示当前时间 }
转自:http://www.jb51.net/article/44713.htm
Fundebug是前端JavaScript错误实时监控平台。Fundebug的JavaScript监控插件已经能够在各种主流浏览器中自动捕获错误,并且可以获取最全面的错误信息,帮助开发者更快的Debug。