37

When a JavaScript exception is thrown in IE 8, how can I see its stack trace?

For example, the following code from jQuery catches an exception and rethrows it. Debugging in Visual Studio (2012), execution breaks as the exception ('e') is caught by jQuery, but I can't for the life of me see the stack trace of where the exception originates from:

// resolve with given context and args
resolveWith: function( context, args ) {
    if ( !cancelled && !fired && !firing ) {
        firing = 1;
        try {
            while( callbacks[ 0 ] ) {
                callbacks.shift().apply( context, args );
            }
        }
        // We have to add a catch block for
        // IE prior to 8 or else the finally
        // block will never get executed
        catch (e) {
            throw e;
        }
        finally {
            fired = [ context, args ];
            firing = 0;
        }
    }
    return this;
}

I've tried the stacktrace.js library, but it seems to ignore the exception when the browser is IE 8, just falling back to producing the stack trace of the current frame.

EDIT:

As you can see from the below screenshot, the exception has no properties pertaining to the stack:

JavaScript exception object

aknuds1
  • 65,625
  • 67
  • 195
  • 317
  • 2
    Can't you put a break point in the `catch` block and analyze the Error object *before* it is re-thrown? – Šime Vidas Apr 04 '13 at 12:15
  • @ŠimeVidas The Error object has no stack trace, you see. It has a message and a number, but no trace. – aknuds1 Apr 04 '13 at 12:28
  • In Internet Explorer *Error* objects have a `.stack` property: http://jsfiddle.net/2HDBf/ (As a matter of fact, that property is cross-browser.) – Šime Vidas Apr 04 '13 at 13:14
  • @ŠimeVidas And you have tested this in IE 8? I am testing now, and there is no .stack property on the exception. See my screenshot for reference. Btw, jsFiddle won't display properly for me in IE 8, so I can't try your fiddle. – aknuds1 Apr 04 '13 at 13:37
  • No, of course not. IE is at version 10 already. Why haven't you updated IE? IE8's dev tools are crap compared to what IE has now. – Šime Vidas Apr 04 '13 at 16:30
  • 2
    @ŠimeVidas As you can see the question is about IE 8. Some of us are forced to support users with older browsers. – aknuds1 Apr 04 '13 at 16:38
  • But that does not mean that you personally have to use IE8. IE's dev tools have "Browser mode" - you can set it to "IE8" to test your site in that version of IE. – Šime Vidas Apr 04 '13 at 16:45
  • 1
    @ŠimeVidas We find that we cannot reproduce all IE 8's quirks in IE 10 despite the browser mode. Therefore we must test with IE 8. – aknuds1 Apr 04 '13 at 17:27
  • I see. Well, consider having at least one machine with IE10 on it. If you have multiple work machines, or desktop + laptop combinations, I'd put IE10 on one of those. As I said, IE8's dev tools are crap. – Šime Vidas Apr 04 '13 at 21:10
  • @ŠimeVidas Yeah, we test with a number of browsers, but IE 8 is the most important to test with in that it's so tricky to get right. – aknuds1 Apr 04 '13 at 21:31
  • @EricWendelin Thanks, will give TraceKit a shot – aknuds1 Apr 09 '13 at 21:20

2 Answers2

0

In Internet Explorer 8 and earlier versions, you can use the error object to get the stack trace of a thrown exception. Here's an example of how you can modify the code to log the stack trace:

resolveWith: function( context, args ) {
if ( !cancelled && !fired && !firing ) {
    firing = 1;
    try {
        while( callbacks[ 0 ] ) {
            callbacks.shift().apply( context, args );
        }
    }
    // We have to add a catch block for
    // IE prior to 8 or else the finally
    // block will never get executed
    catch (e) {
        if (e.stack) {
            console.log(e.stack);
        }
        throw e;
    }
    finally {
        fired = [ context, args ];
        firing = 0;
    }
}
return this;
 }

In the catch block, you check if the stack property exists on the error object. If it does, you log it to the console. If not, you fall back to just rethrowing the exception. This should allow you to see the stack trace in the console when debugging in Visual Studio.

safXcode
  • 46
  • 4
-2

doesn't this work!!

function getStackTrace(e) {
  var stack = [];
  while (e) {
    stack.push({
      fileName: e.fileName,
      lineNumber: e.lineNumber,
      name: e.name,
      message: e.message,
      stack: e.stack
    });
  }
  return stack;
}
Anuj Shah
  • 537
  • 4
  • 11