在错误对象相关性的JavaScript中抛出错误 [英] Throwing errors in Javascript with error object relevancy
问题描述
这几乎是IE相关的,因为IE是我用来测试这个的环境,但是我想知道当你抛出错误时是否会影响错误对象属性的相关性。请考虑以下javascript:
This is pretty much IE related because IE is the environment I'm using to test this, but I want to know if you can affect the relevancy of the error object properties when you throw an error. Consider the following javascript:
function MyClass (Arg1, Arg2) // Line 5 of my.js
{
if (typeof Arg1 != "string")
throw new Error("Invalid argument passed for MyClass");
// Do some other stuff here
}
进一步下来你的代码有
var myvar = new MyClass(100, "Hello"); // Line 3201 of my.js
所以上面会抛出一个错误,但是报错调试信息会显示在my.js的第9行抛出的错误,而不是第320行。这是可以使用标准方法更改的?
So the above would throw an error, but the error reported in the debugging information would show the error being thrown at line 9 of my.js instead of line 3201. Is this something you can change using standard methods?
推荐答案
你实际寻找的是一个堆栈跟踪的错误。没有这个标准,但大多数浏览器都提供了一些发现的方法。快速搜索会出现这个 js堆栈跟踪示例。
What you are actually looking for is a stack trace for the error. There are no standards for this but most browsers do provide some means of discovery. Doing a quick search comes up with this js stack trace example.
这篇关于在错误对象相关性的JavaScript中抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!