views:

3488

answers:

5

If I throw a Javascript exception myself (eg, throw "AArrggg"), how can I get the stack trace (in Firebug or otherwise)? Right now I just get the message.

edit: As many people below have posted, it is possible to get a stack trace for a JavaScript exception but I want to get a stack trace for my exceptions. For example:

function foo() {
    bar(2);
}
function bar(n) {
    if (n < 2)
        throw "Oh no! 'n' is too small!"
    bar(n-1);
}

When foo is called, I want to get a stack trace which includes the calls to foo, bar, bar.

+4  A: 

I don't think there's anything built in that you can use however I did find lots of examples of people rolling their own.

Mark Biek
Ah, thanks -- the first link there seems like it may do (although the lack of recursion support may render it unworkable).
David Wolever
Yeah, I didn't see any that supported recursion on first glance. I'll be curious to see if there's a good solution to that.
Mark Biek
I think the second link should support recursion for Firefox and Opera because it uses the error stack trace rather than manually building one using the arguments variable. I'd love to hear if you find a cross browser solution for the recursion issue (the first article is mine). :)
Helephant
Helephant: The second won't work here because, when I catch the exception, it's a "string" (ie, no "e.stack"):foo = function(){ throw "Arg"; }try { foo(); } catch (e) { /* typeof e == "string" */ }Maybe I'm throwing it wrong?(begin obligatory rant about how stupid Javascript tutorials are...)
David Wolever
+7  A: 
Helephant
Hrm, that doesn't seem to work. It stops me in a debugger on errors raised by Javascript (eg, undefined variable errors), but when I throw my own exceptions I still don't get anything but the "Uncaught exception" message.
David Wolever
A: 

It is easier to get a stack trace on Firefox than it is on IE but fundamentally here is what you want to do:

Wrap the "problematic" piece of code in a try/catch block:

try {
    // some code that doesn't work
    var t = null;
    var n = t.not_a_value;
}
    catch(e) {
}

If you will examine the contents of the "error" object it contains the following fields:

e.fileName : The source file / page where the issue came from e.lineNumber : The line number in the file/page where the issue arose e.message : A simple message describing what type of error took place e.name : The type of error that took place, in the example above it should be 'TypeError' e.stack : Contains the stack trace that caused the exception

I hope this helps you out.

Michael
Wrong.He's trying to catch his OWN exceptions. If he throws "asdfg", he'll get string object, not an exception object. He's not trying to catch built-in exceptions.
Ivan Vučica
+2  A: 

This snippet may somewhat help, although I didn't try it yet.

Eugene Morozov
This seems to be the most promising so far... Creating an 'Exception', so calling an exception would be: throw Exception('message'), where the Exception function would figure out what's on the stack.
David Wolever
Alright, well, since this is the closest thing to a correct answer, I'll accept it.
David Wolever
+1  A: 

You can access the stack (stacktrace in Opera) properties of an Error instance even if you threw it. The thing is, you need to make sure you use throw new Error(string) (don't forget the new instead of throw string.

Example:

try {
    0++;
} catch (e) {
    var myStackTrace = e.stack || e.stacktrace || "";
}
Eli Grey
stacktrace doesn't work in Opera. I can't even find something about it.
NV
@NV: It seems stacktrace isn't on user-created errors so you should do this instead:try { 0++ } catch(e) { myStackTrace=e.stack || e.stacktrace }
Eli Grey
It works, thanks!
NV