如何在Internet Explorer中获取javascript堆栈跟踪.e.stack 返回 “Undefined”

How to get javascript stack trace in Internet Explorer. e.stack returns "Undefined"

本文关键字:stack 跟踪 返回 Undefined 堆栈 javascript Internet Explorer 获取      更新时间:2023-09-26

我尝试了以下代码

try {
  alertt("dddd");
} catch (e) {
    console.log(e.stack);
}

它在Google Chrome和Mozilla Firefox中产生堆栈跟踪。但它在 Internet Explorer 中返回未定义

有什么方法可以在IE浏览器中获取堆栈跟踪吗?

您的代码绝对适用于IE11;我刚刚试过了。我相信它也应该至少在IE10中工作。

您可能还对console.trace感兴趣,它为您提供了堆栈跟踪。这在IE11中绝对是新的,但这只是使它成为升级的另一个很好的理由 - IE11中的开发工具比以前好几个数量级。

您可以使用 MSDN 文档中提到的e.description

语法 :

errorObj = new Error()
errorObj = new Error([number])
errorObj = new Error([number[, description]])

参数说明:

错误对象

Required. The variable name to which the Error object is assigned. The variable assignment is omitted when you create the error using a throw statement.

Optional. Numeric value assigned to an error. Zero if omitted.

描述

Optional. Brief string that describes an error. Empty string if omitted.

function checkInput(x) {
    try
    {
        if (isNaN(parseInt(x))) {
            throw new Error("Input is not a number.");
        }
    }
    catch(e)
    {
        document.write(e.description);
    }
}
checkInput("not a number");

注意:每当发生运行时错误时,都会创建 Error 对象的实例来描述错误。此实例具有两个内部属性,其中包含error (description property)error number (number property)的说明。有关详细信息,请参阅 http://msdn.microsoft.com/en-us/library/ie/1dk3k160%28v=vs.94%29.aspx。

错误号是 32 位值。上面的 16 位字是设施代码,而下面的字是实际的错误代码。