【问题标题】:Logging try{ ... } catch(e){ ... } using ajax : possible?使用 ajax 记录 try{ ... } catch(e){ ... } :可能吗?
【发布时间】:2011-11-17 22:11:28
【问题描述】:
当我在 chrome 控制台中尝试以下操作时,我得到 'undefined' 作为 'e' 的值:
try{
var test=somethingInvalid();
}
catch(e){
console.log(e);
}
如何访问 e 的详细信息?
它有什么属性?
谢谢。
哦。做 console.log('E is: '+e) 接缝工作。对不起!
【问题讨论】:
标签:
javascript
ajax
exception
logging
【解决方案1】:
我有一个自定义的assert() 函数,它可以提取函数的堆栈跟踪(在所有现代浏览器中,IE 除外):
if (!condition) {
try {
throw Error();
} catch (e) {
// Browsers Firefox, Chrome, Opera provide additional stack property;
// Check if it does not exist (IE);
stackTrace = (!e.stack) ? "No stack available" : e.stack;
self.reportError(errorType, stackTrace, message);
}
}
很明显,发生的事情比我所展示的要多得多,但想法就在那里。
【解决方案2】:
尽管提供的所有答案都非常有帮助,但我们实际上正在寻找的是:
try{
var test=somethingThatDoesntExist();
}
catch(e){
console.log(e.message);
}
e.message 是我们要记录的变量,因为它(据我们所知)包含已捕获错误的字符串表示形式。
谢谢!
【解决方案3】:
据我所知,Chrome 确实输出了“e”的详细信息。我刚刚这样做了:
var err;
try { /* what you did */ }
catch (e) { err = e; }
Chrome 会在哪个页面上显示“e”的详细信息。
在这种情况下,您可以执行调用以查看以下内容:
- err.name
- err.arguments
- err.type
- err.stack
祝你好运!
【解决方案4】:
嗯,它会返回undefined,因为最后一行console.log 没有返回值。
try{
false = true;
}
catch(e){
console.log(e);
}
但使用上述内容,您还将看到一个对象> Reference Error,后跟undefined 的下一行