简体   繁体   中英

How can I get the stack trace for a JavaScript exception in IE 8?

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 异常对象

Take a look in this link: DIY javascript stack trace

His example is working on my IE8.

To see the stack trace of where the exception originates from when debugging in Visual Studio 2012, you can use the Debugger object to break execution and inspect the stack trace.

Here is an example of how you can do this:

// 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) {
            // Break execution and inspect the stack trace
            Debugger.break();
            throw e;
        }
        finally {
            fired = [ context, args ];
            firing = 0;
        }
    }
    return this;
}

This code will break execution and open the Visual Studio debugger when the exception is caught. You can then inspect the stack trace and see where the exception originated from.

Keep in mind that this will only work if the Visual Studio debugger is attached to the process. If the debugger is not attached, execution will not be broken and you will not be able to see the stack trace.

Does this help?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM