开发者

Throwing errors in Javascript with error object relevancy

开发者 https://www.devze.com 2022-12-08 09:00 出处:网络
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

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
}

Further down your code you have

var myvar = new MyClass(100, "Hello");    // Line 3201 of my.js

So the above would throw an error, but the error reported in the debug开发者_运维技巧ging 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?


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.


In firefox you can use the stack property when you catch the error. In other browsers you can use the message property.

Have a look at this link on how to catch the stacktrace.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号