admin管理员组文章数量:1356215
I have a custom exception class which I'm using for a particular situation. It could be thrown from anywhere so try/catch isn't really practical.
throw new CustomException;
I want to catch this error in window.onerror and filter it out, which works fine in most browsers I've tested so far.
var window_onerror = window.onerror || function() {return false;};
window.onerror = function(message, url, line) {
if (message.match(CustomException.prototype.name)) {
return true;
} else {
return window_onerror(message, url, line);
}
};
However, in IE the window.onerror function receives Exception thrown and not caught
instead of my custom exception.
I have a custom exception class which I'm using for a particular situation. It could be thrown from anywhere so try/catch isn't really practical.
throw new CustomException;
I want to catch this error in window.onerror and filter it out, which works fine in most browsers I've tested so far.
var window_onerror = window.onerror || function() {return false;};
window.onerror = function(message, url, line) {
if (message.match(CustomException.prototype.name)) {
return true;
} else {
return window_onerror(message, url, line);
}
};
However, in IE the window.onerror function receives Exception thrown and not caught
instead of my custom exception.
3 Answers
Reset to default 2We have a universal Exception handler which we use on Exceptioneer. however each browser behaves differently and reports the same exceptions in different ways.
Also, different localised versions of browser act in different ways, for example, I've seen Javascript errors in Russian from some of our users - not the easiest thing in the world to parse.
This script will let you see how different browsers act to errors: -
window.onerror = function(message, uri, line) {
var fullMessage = location.href + '\n' + uri + '\n' + line;
alert(fullMessage);
return false;
}
Thanks,
Phil.
Declare a global variable and some enumerations..
var errorCode;
const ErrCustomException = 1, ... ;
Put a line before your throw exception..
errorCode = ErrCustomException;
throw new CustomException;
Change the condition to this..
window.onerror = function(message, url, line) {
if (errorCode == ErrCustomException) {
return true;
} else {
return window_onerror(message, url, line);
}
};
I don't know of any way to retrieve the thrown object within the onerror
handler. As a workaround, I suggest throwing a generic runtime error with a custom message, ie
throw new Error('foo')
and check
message === 'foo'
inside the handler function.
edit: working example code:
window.onerror = function(message, url, line) {
alert(message === 'foo');
return true;
};
throw new Error('foo');
本文标签: internet explorerCatching a custom javascript exception with windowonerrorin IEStack Overflow
版权声明:本文标题:internet explorer - Catching a custom javascript exception with window.onerror - in IE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743950805a2567255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论