[英]How to retrieve try/catch error
how can I find the error 我如何找到错误
try {
undef
} catch (e){
console.log(e)
console.dir(e)
}
the information bust be there somewhere, because the console.log (in firebug) includes: 信息崩溃位于某处,因为console.log(在Firebug中)包括:
ReferenceError: undef is not defined
but when I browse the e
object, I can't find it. 但是当我浏览
e
对象时,找不到它。
How do I find out what the error is programmatically, so I can handle the error accordingly? 如何以编程方式找出错误所在,以便可以相应地处理错误?
Edit: 编辑:
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]);
}
}
Which will produce: 会产生:
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
I don't think you can pull its type explicitly, but you can test it: 我认为您不能显式提取其类型,但是可以对其进行测试:
try {
undef
} catch (e){
console.log(e)
console.dir(e)
if(e instanceof ReferenceError) {
console.log("Ooops! Fat fingers!");
}
}
But of trial and error gives me this... 但是反复试验给了我这个...
try {
undef
} catch (e){
console.log(e.toString())
// ReferenceError: undef is not defined
}
I guess firebug is just accessing the .toString()
method of e
我想萤火虫只是访问
e
的.toString()
方法
Edit: 编辑:
I am guessing the .toString()
method just concatenates the only two properties that are guaranteed cross browser - name
and message
- so I think e.message
is the only reliable and useful piece of information to go with. 我猜想
.toString()
方法只是连接了保证跨浏览器的仅有两个属性- name
和message
-因此我认为e.message
是唯一可靠且有用的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.