如果我自己抛出一个JavaScript异常(例如,抛出“AArrggg”),我如何获得堆栈跟踪(在Firebug或其他)?现在我刚收到消息。

编辑:正如下面许多人发布的那样,可以为JavaScript异常获得堆栈跟踪,但我想为我的异常获得堆栈跟踪。例如:

function foo() {
    bar(2);
}
function bar(n) {
    if (n < 2)
        throw "Oh no! 'n' is too small!"
    bar(n-1);
}

当调用foo时,我想获得一个堆栈跟踪,其中包括对foo, bar, bar的调用。


当前回答

在Firefox上比在IE上更容易获得堆栈跟踪,但从根本上来说,这是你想要做的:

将“有问题的”代码段包装在try/catch块中:

try {
    // some code that doesn't work
    var t = null;
    var n = t.not_a_value;
}
    catch(e) {
}

如果你要检查"error"对象的内容,它包含以下字段:

e.fileName:问题产生的源文件/页 e.lineNumber:出现问题的文件/页中的行号 message:描述发生错误类型的简单消息 e.name:发生错误的类型,在上面的例子中它应该是'TypeError' e.stack:包含导致异常的堆栈跟踪

我希望这能帮到你。

其他回答

编辑2 (2017):

在所有现代浏览器中,您可以简单地调用:console.trace();(MDN引用)

编辑1 (2013):

一个更好(也更简单)的解决方案是使用Error对象的stack属性,就像在原始问题的评论中指出的那样:

function stackTrace() {
    var err = new Error();
    return err.stack;
}

这将产生如下输出:

DBX.Utils.stackTrace@http://localhost:49573/assets/js/scripts.js:44
DBX.Console.Debug@http://localhost:49573/assets/js/scripts.js:9
.success@http://localhost:49573/:462
x.Callbacks/c@http://localhost:49573/assets/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost:49573/assets/js/jquery-1.10.2.min.js:4
k@http://localhost:49573/assets/js/jquery-1.10.2.min.js:6
.send/r@http://localhost:49573/assets/js/jquery-1.10.2.min.js:6

给出调用函数的名称以及URL、它的调用函数等等。

原(2009):

这段代码的修改版本可能会有所帮助:

function stacktrace() { 
  function st2(f) {
    return !f ? [] : 
        st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']);
  }
  return st2(arguments.callee.caller);
}

对Eugene的回答进行了更新:为了让IE(特定版本?)填充堆栈属性,必须抛出错误对象。下面的例子应该比他现在的例子工作得更好,并且应该避免在IE中返回undefined。

function stackTrace() {
  try {
    var err = new Error();
    throw err;
  } catch (err) {
    return err.stack;
  }
}

注1:这类事情只应该在调试时执行,在运行时禁用,特别是频繁调用时。注2:这可能不能在所有浏览器中工作,但似乎可以在FF和IE 11中工作,这很适合我的需求。

此填充代码在现代(2017)浏览器(IE11, Opera, Chrome, FireFox, Yandex)中工作:

printStackTrace: function () {
    var err = new Error();
    var stack = err.stack || /*old opera*/ err.stacktrace || ( /*IE11*/ console.trace ? console.trace() : "no stack info");
    return stack;
}

其他答案:

function stackTrace() {
  var err = new Error();
  return err.stack;
}

不能在IE 11中工作!

使用arguments. called .caller -在任何浏览器中都不能在严格模式下工作!

这里有一个答案,给你最大的性能(IE 6+)和最大的兼容性。兼容IE 6!

function stacktrace( log_result ) { var trace_result; // IE 6 through 9 compatibility // this is NOT an all-around solution because // the callee property of arguments is depredicated /*@cc_on // theese fancy conditinals make this code only run in IE trace_result = (function st2(fTmp) { // credit to Eugene for this part of the code return !fTmp ? [] : st2(fTmp.caller).concat([fTmp.toString().split('(')[0].substring(9) + '(' + fTmp.arguments.join(',') + ')']); })(arguments.callee.caller); if (log_result) // the ancient way to log to the console Debug.write( trace_result ); return trace_result; @*/ console = console || Console; // just in case if (!(console && console.trace) || !log_result){ // for better performance in IE 10 var STerror=new Error(); var unformated=(STerror.stack || STerror.stacktrace); trace_result = "\u25BC console.trace" + unformated.substring(unformated.indexOf('\n',unformated.indexOf('\n'))); } else { // IE 11+ and everyone else compatibility trace_result = console.trace(); } if (log_result) console.log( trace_result ); return trace_result; } // test code (function testfunc(){ document.write( "<pre>" + stacktrace( false ) + "</pre>" ); })();

使用console.error(e.stack) Firefox只在日志中显示堆栈跟踪, Chrome也会显示这条消息。 如果邮件中包含重要信息,这可能是一个糟糕的惊喜。两者都要记录日志。