看看这个小提琴:
http://jsfiddle.net/xYsRA/1/
window.onerror = function (msg, url, line) {
console.log("Caught[via window.onerror]: '" + msg + "' from " + url + ":" + line);
return true; // same as preventDefault
};
window.addEventListener('error', function (evt) {
console.log("Caught[via 'error' event]: '" + evt.message + "' from " + evt.filename + ":" + evt.lineno);
console.log(evt); // has srcElement / target / etc
evt.preventDefault();
});
throw new Error("Hewwo world. I crash you!!!");
throw new Error("Hewwo world. I can only crash you once... :(");
哪个打印:
捕获[通过 window.onerror]:'未捕获的错误:Hewwo 世界。我让你崩溃!!!来自 http://fiddle.jshell.net/xYsRA/1/show/:32 fiddle.jshell.net:21
Caught[via 'error' event]: 'Uncaught Error: Hewwo world. 我让你崩溃!!!来自 http://fiddle.jshell.net/xYsRA/1/show/:32 fiddle.jshell.net:26
ErrorEvent {lineno: 32, filename: "http://fiddle.jshell.net/xYsRA/1/show/", message: "Uncaught Error: Hewwo world. I crash you!!!", clipboardData: undefined, cancelBubble:错误的…}
fiddle.jshell.net:27\
笔记: