我需要从JavaScript异常中获取fileName、Message、LineNumber等信息。我尝试了以下代码。
在Mozilla Firefox中,我得到了以下结果:
我尝试过e.stack,它将堆栈跟踪返回为字符串。在Google Chrome中,它给了我以下输出:
并且火狐浏览器给出了这个结果。
try {
alertt("dddd");
} catch (e) {
console.log("ExceptionType: "+ e.name);
console.log("Message: "+ e.message);
console.log("Line No: "+ e.lineNumber);
}
在Mozilla Firefox中,我得到了以下结果:
但相同的代码在Google Chrome和Internet Explorer中给出了以下结果:异常类型:ReferenceError
错误信息:alertt未定义
行号:4
它没有给出行号。如何解决这个问题?还有没有其他方法可以获取行号?异常类型:ReferenceError
错误信息:alertt未定义
行号:未定义
我尝试过e.stack,它将堆栈跟踪返回为字符串。在Google Chrome中,它给了我以下输出:
ReferenceError: alertt is not defined
at message (http://localhost/ems-test/js/test.js:4:4)
at HTMLDocument.<anonymous> (http://localhost/ems-test/js/test.js:14:2)
at c (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26036)
at Object.p.fireWith [as resolveWith] (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26840)
at Function.x.extend.ready (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:3305)
at HTMLDocument.q (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:717)
并且火狐浏览器给出了这个结果。
message@http://localhost/ems-test/js/test.js:4
@http://localhost/ems-test/js/test.js:14
x.Callbacks/c@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
.ready@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
q@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
两个都是字符串类型的结果。不是一个对象。所以需要从这个巨大的字符串中提取行号。但问题是两个结果不同。一个在第一行显示行号,另一个在第二行显示行号。因此从这个巨大的字符串中提取行号将会很困难。
有没有办法将堆栈跟踪作为对象获取?