測試代碼:
在IE下:
在FireFox下
在safari下:
try{abc();}catch(e){
for(var i in e){
document.write("<p><b>"+i+"</b>:"+e[i]+"</p>");
}
}
for(var i in e){
document.write("<p><b>"+i+"</b>:"+e[i]+"</p>");
}
}
在IE下:
name:TypeError
message:缺少對象
number:-2146823281
description:缺少對象
在FireFox下
message:abc is not defined
fileName:file:///C:/test.html
lineNumber:14
stack:@file:///C:/test.html:14
name:ReferenceError
在safari下:
message:Can't find variable: abc
line:14
sourceId:4
sourceURL:file:///C:/test.html
name:ReferenceError
最爛的是opera:
message:Statement on line 4: Reference to undefined variable: abc Backtrace: Line 4 of inline#1 script in file://localhost/C:/test.html abc();
opera#sourceloc:4
雖然message是所有瀏覽器都支持,但是所有瀏覽器的message都不同,而且不同語言版本的操作系統和瀏覽器也有不同。
因此要做跨瀏覽器的異常區分處理,emu認為最好是用name來區分異常類型,這樣就只有瀏覽器的不同而沒有語言版本的不同了。opera做的這么爛,以至于只能從message入手,還是最后再考慮做對它的支持吧。