我怎麼能找到錯誤如何檢索的try/catch錯誤
try {
undef
} catch (e){
console.log(e)
console.dir(e)
}
信息半身像是在某個地方,因爲執行console.log(螢火蟲)包括:
ReferenceError: undef is not defined
但是當我瀏覽了e
對象,我找不到它。
如何以編程方式查明錯誤是什麼,以便我可以相應地處理錯誤?
編輯:
我怎麼能找到錯誤如何檢索的try/catch錯誤
try {
undef
} catch (e){
console.log(e)
console.dir(e)
}
信息半身像是在某個地方,因爲執行console.log(螢火蟲)包括:
ReferenceError: undef is not defined
但是當我瀏覽了e
對象,我找不到它。
如何以編程方式查明錯誤是什麼,以便我可以相應地處理錯誤?
編輯:
try {
if(typeof undef == 'undefined'){
console.log('We should not access this "undef" var');
}
console.log('The next line will produce an exception');
undef
} catch (e){
console.log(e);
for(index in e){
console.log(index+' ('+(typeof e[index])+'): '+ e[index]);
}
}
將產生:
We should not access this "undef" var The next line will produce an exception ReferenceError: undef is not defined fileName (string): file:///B:/xampp/htdocs/study/test.html lineNumber (number): 12 stack (string): @file:///B:/xampp/htdocs/study/test.html:12
請將'e.toString()'添加到此答案以獲得完整性 – 2012-08-10 19:46:14
我不認爲你可以明確地拉它的類型,但你可以測試一下:
try {
undef
} catch (e){
console.log(e)
console.dir(e)
if(e instanceof ReferenceError) {
console.log("Ooops! Fat fingers!");
}
}
但審判和錯誤給了我這個...
try {
undef
} catch (e){
console.log(e.toString())
// ReferenceError: undef is not defined
}
我想螢火蟲只是訪問.toString()
方法e
編輯:
我猜.toString()
方法只是串接是保證跨瀏覽器的唯一兩個屬性 - name
和message
- 所以我覺得e.message
是唯一可靠的和有用的資料片一起去。
在我的瀏覽器它就在那兒,在'type': 「not_defined」,和'arguments'是一個數組瓦特/元素0 =' 「是undef」'。你是否特意提到你沒有產生的例外? – 2012-08-10 19:11:29
我添加了一個截圖 - 你的看起來像那樣嗎?我在OSX上使用firefox 13.0.1和firebug 1.10.2 – 2012-08-10 19:16:30
@BillyMoon這是我在Chrome開發工具中看到的http://i.stack.imgur.com/E3O34.png – sachleen 2012-08-10 19:19:30