我有一個自定義異常類,我正在使用的特定情況。它可以從任何地方拋出,所以嘗試/捕捉並不真實。在window.onerror中捕獲一個自定義的JavaScript異常 - 在IE中
throw new CustomException;
我要趕在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);
}
};
然而,在IE中window.onerror函數接收Exception thrown and not caught
,而不是我的自定義異常。
我會授予你它在IE中的作用(這是我問的),但Firefox只報告'Script Error'。 – 2009-11-18 22:41:45
@Justin:對我很好 - 我們可以看到一些代碼嗎? – Christoph 2009-11-18 22:56:19
http://bitbucket.org/JustinLove/module/src/4f80d66afb94/請參閱在qunit/index.html中測試的script/cgd/Module.js拋出在第126行,異常定義和處理器從196開始。 – 2009-11-19 17:23:32