JavaScript 异常处理 - 显示行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1901012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:49:21  来源:igfitidea点击:

JavaScript exception handling - displaying the line number

javascriptexception-handlingcallstack

提问by Justin

When catching / handling exceptions in JavaScript, how can I determine what the call stack was when the exception occurred? (and also if possible what the line number was)

在 JavaScript 中捕获/处理异常时,如何确定发生异常时的调用堆栈?(如果可能的话,行号是什么)

try
{
    // etc...
}
catch (ex)
{
    // At this point here I want to be able to print out a detailed exception 
    // message, complete with call stack, and if possible line numbers.
}

采纳答案by Chris Clark

Each browser handles this differently, so there isn't a universal way to do it. This blog posthas some good code to dump a stack trace for most supported browsers. I don't think there is a good way to provide the line number.

每个浏览器对此的处理方式不同,因此没有通用的方法来做到这一点。这篇博文有一些很好的代码可以为大多数支持的浏览器转储堆栈跟踪。我认为没有提供行号的好方法。

If you're looking to debug one function in particular, Firebughas a good stack trace function (vis console.trace()).

如果您特别想调试一个函数,Firebug有一个很好的堆栈跟踪函数(vis console.trace())。

回答by jldupont

Have a look at this.

看看这个

A way to analyse the available information:

一种分析可用信息的方法:

try 
{ 
    doInit(); 
} catch(err) 
{ 
    var vDebug = ""; 
    for (var prop in err) 
    {  
       vDebug += "property: "+ prop+ " value: ["+ err[prop]+ "]\n"; 
    } 
    vDebug += "toString(): " + " value: [" + err.toString() + "]"; 
    status.rawValue = vDebug; 
}

回答by Justin

I've discovered that in JavaScript running under IE it is not possible to capture a stack trace at the point that an exception is caught. According to thisPDF the only way of getting a stack trace in IE is if you don't handle the exception.

我发现在 IE 下运行的 JavaScript 中,不可能在捕获异常时捕获堆栈跟踪。根据PDF,在 IE 中获取堆栈跟踪的唯一方法是不处理异常。