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:
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:
Share Improve this question edited Apr 4, 2013 at 13:36 aknuds1 asked Apr 4, 2013 at 11:55 aknuds1aknuds1 68k69 gold badges205 silver badges320 bronze badges 11 | Show 6 more comments3 Answers
Reset to default 1
function someFunction() {
try {
// Code that may throw an exception
} catch (e) {
logStackTrace(e);
}
}
function logStackTrace(error) {
var stackTrace = [];
var caller = arguments.callee.caller;
while (caller) {
stackTrace.push(caller.name || caller.toString());
caller = caller.caller;
}
error.stack = stackTrace.join("\n");
console.error(error);
}
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.
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;
}
catch
block and analyze the Error object before it is re-thrown? – Šime Vidas Commented Apr 4, 2013 at 12:15.stack
property: jsfiddle.net/2HDBf (As a matter of fact, that property is cross-browser.) – Šime Vidas Commented Apr 4, 2013 at 13:14