catching error event message from firefox

你说的曾经没有我的故事 提交于 2019-12-05 20:34:12

I modified your code a little as a demo:

function handleException(e) { 
    console.log(e);
    alert(e);
    return false;
}
window.addEventListener("error", handleException, false);

try {
  throw new Error('sdasd');
}
catch (e) {
  console.log(e)
}
console.log('after exception 1');
throw new Error('foo');
console.log('after exception 2');

Running this code (in Firebug) showed me this:

Error: sdasd
[Break On This Error]   

Filtered chrome url chrome://firebug/content/console/commandLineExposed.js

comman...osed.js (line 175)
<System>

after exception 1

"Error: foo `    throw new Error('foo');` @14"

If you're trying to catch an error, use try {...} catch { ...}. It looks like you're just binding to an error event, so the exception you're throwing will still propagate up to window and tell the JS engine to halt. Run this code in Chrome, you'll see that you never see "after exception 2", but you will see "after exception 1".

The purpose of exceptions (created by throw) is to stop code execution unless there's code made to handle that particular exception. You can see an example on the MDN page for try-catch

Edit: it just occurred to me that you might be trying to catch a jQuery error event. If this is the case, your event binding is correct but you shouldn't be testing it with throw

Edit 2: I should've noticed this sooner, but you're trying to listen for a DOM error event with window.addEventListener. Error events will not break execution. Exceptions do.

Replace your code throw new Error('sdasd'); with this code:

var customEvent = new CustomEvent('error')

window.dispatchEvent(customEvent);

That should solve your problem. I used the MDN page on custom events as a reference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!