如何找到错误
try {
undef
} catch (e){
console.log(e)
console.dir(e)
}
信息破灭在某处,因为console.log(在firebug中)包括:
ReferenceError: undef is not defined
但是当我浏览e
对象时,我找不到它。
如何以编程方式找出错误,以便我可以相应地处理错误?
编辑:
答案 0 :(得分:1)
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
答案 1 :(得分:0)
我认为你不能明确地提取它的类型,但你可以测试它:
try {
undef
} catch (e){
console.log(e)
console.dir(e)
if(e instanceof ReferenceError) {
console.log("Ooops! Fat fingers!");
}
}
答案 2 :(得分:0)
但是反复试验给了我这个......
try {
undef
} catch (e){
console.log(e.toString())
// ReferenceError: undef is not defined
}
我想firebug只是访问.toString()
e
方法
编辑:
我猜测.toString()
方法只是连接两个保证跨浏览器的属性 - name
和message
- 所以我认为e.message
是唯一可靠的有用的信息。