I need to run third party JS to scan jserrors. I am running the following in chrome's console
window.onerror = function(errorMsg, url, lineNumber) {
alert(errorMsg);
}
throw new Error('foo');
I expect both the console to inform me of the error and a alert to pop up displaying the error message. The alert is never fired, and I assume the event is not being fired. why not?
The Chrome Developer Console won't call the user-defined error event listener if the error is thrown immediately (according to this answer by Paul S.).
Throwing the error "just in time":
Throwing the error deferred:
(xxxx is the specific return value of
setTimeout()
)Here is the original code snippet which led me to the point that the Chrome Dev Console changes the internal behaviour somehow:
This code is to be executed in a normal HTML file.