Javascript 如何检索try/catch错误
我怎样才能找到错误呢Javascript 如何检索try/catch错误,javascript,Javascript,我怎样才能找到错误呢 try { undef } catch (e){ console.log(e) console.dir(e) } 信息可能在某个地方,因为console.log(在firebug中)包括: ReferenceError: undef is not defined 但是当我浏览e对象时,我找不到它 如何以编程方式找出错误,以便相应地处理错误 编辑: 我认为您不能显式地提取它的类型,但您可以测试它: try { undef } catch (
try {
undef
} catch (e){
console.log(e)
console.dir(e)
}
信息可能在某个地方,因为console.log(在firebug中)包括:
ReferenceError: undef is not defined
但是当我浏览e
对象时,我找不到它
如何以编程方式找出错误,以便相应地处理错误
编辑:
我认为您不能显式地提取它的类型,但您可以测试它:
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
}
我猜firebug只是在访问e
编辑: 我猜
.toString()
方法只是连接了跨浏览器保证的仅有两个属性-name
和message
——因此我认为e.message
是唯一可靠和有用的信息
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]);
}
}
这将产生:
我们不应该访问这个“未定义”变量 下一行将生成一个异常 ReferenceError:未定义undef 文件名(字符串):file:///B:/xampp/htdocs/study/test.html 行号(编号):12 堆栈(字符串):@file:///B:/xampp/htdocs/study/test.html:12
在我的浏览器中,它就在那里,在
类型中
:“未定义”,而参数
是一个数组,其中包含元素0=“未定义”
。你是指你没有生成的异常吗?我添加了一个截图-你的截图看起来像这样吗?我在OSX上使用firefox 13.0.1和firebug 1.10。2@BillyMoon这就是我在Chrome开发工具中看到的,返回的错误对象是跨浏览器捕获标准的吗?为了完整性,请在回答中添加e.toString()
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