Jus*_*ove 5 javascript internet-explorer exception
我有一个用于特定情况的自定义异常类。它可以从任何地方抛出,因此 try/catch 并不实用。
throw new CustomException;
Run Code Online (Sandbox Code Playgroud)
我想在 window.onerror 中捕获此错误并将其过滤掉,这在我迄今为止测试过的大多数浏览器中都可以正常工作。
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);
}
};
Run Code Online (Sandbox Code Playgroud)
但是,在 IE 中 window.onerror 函数接收Exception thrown and not caught
而不是我的自定义异常。
我不知道有什么方法可以检索onerror
处理程序中抛出的对象。作为解决方法,我建议使用自定义消息抛出通用运行时错误,即
throw new Error('foo')
Run Code Online (Sandbox Code Playgroud)
并检查
message === 'foo'
Run Code Online (Sandbox Code Playgroud)
在处理函数内部。
编辑:工作示例代码:
window.onerror = function(message, url, line) {
alert(message === 'foo');
return true;
};
throw new Error('foo');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8302 次 |
最近记录: |