5

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 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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, I've looking into stack tracing without a debugger and found a pretty useful link eriwen.com/javascript/js-stack-trace. I build Windows Desktop Gadgets (built on IE) and rather than use Visual Studio for debugging I usually just handle window.onerror and alert with the line/error message so I can go straight to it in the code - it's quick and easy enough that way but I can see how seeing the stack could be even more useful.
Just realized your link was the same one - whoops :)
Marked this as the answer because it was the first one. Thanks :)
2

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.

1 Comment

I found that link earlier and it's quite interesting, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.