繁体   English   中英

如何在 IE 8 中获取 JavaScript 异常的堆栈跟踪?

[英]How can I get the stack trace for a JavaScript exception in IE 8?

当 IE 8 抛出 JavaScript 异常时,如何查看其堆栈跟踪?

例如,来自 jQuery 的以下代码捕获异常并重新抛出它。 在 Visual Studio (2012) 中调试,执行中断,因为异常 ('e') 被 jQuery 捕获,但我终究无法看到异常来源的堆栈跟踪:

// resolve with given context and args
resolveWith: function( context, args ) {
    if ( !cancelled && !fired && !firing ) {
        firing = 1;
        try {
            while( callbacks[ 0 ] ) {
                callbacks.shift().apply( context, args );
            }
        }
        // We have to add a catch block for
        // IE prior to 8 or else the finally
        // block will never get executed
        catch (e) {
            throw e;
        }
        finally {
            fired = [ context, args ];
            firing = 0;
        }
    }
    return this;
}

我试过stacktrace.js库,但当浏览器是 IE 8 时它似乎忽略了异常,只是回退到生成当前帧的堆栈跟踪。

编辑:

从下面的屏幕截图中可以看出,异常没有与堆栈相关的属性:

JavaScript 异常对象

看看这个链接: DIY javascript堆栈跟踪

他的例子是我的IE8。

要在 Visual Studio 2012 中调试时查看异常来源的堆栈跟踪,您可以使用调试器 object 中断执行并检查堆栈跟踪。

以下是如何执行此操作的示例:

// resolve with given context and args
resolveWith: function( context, args ) {
    if ( !cancelled && !fired && !firing ) {
        firing = 1;
        try {
            while( callbacks[ 0 ] ) {
                callbacks.shift().apply( context, args );
            }
        }
        // We have to add a catch block for
        // IE prior to 8 or else the finally
        // block will never get executed
        catch (e) {
            // Break execution and inspect the stack trace
            Debugger.break();
            throw e;
        }
        finally {
            fired = [ context, args ];
            firing = 0;
        }
    }
    return this;
}

当捕获到异常时,此代码将中断执行并打开 Visual Studio 调试器。 然后您可以检查堆栈跟踪并查看异常的来源。

请记住,这仅在 Visual Studio 调试器附加到进程时才有效。 如果没有附加调试器,执行不会中断,您将无法看到堆栈跟踪。

这有帮助吗?

这行不通!!

function getStackTrace(e) {
  var stack = [];
  while (e) {
    stack.push({
      fileName: e.fileName,
      lineNumber: e.lineNumber,
      name: e.name,
      message: e.message,
      stack: e.stack
    });
  }
  return stack;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM