6

I am calling a function from the console but when it throws an exception I do not receive a stack trace like I would if the code was executed normally.

Is there a way I can modify my command (perhaps with try/catch) to have it provide me this information?

to clarify:

page.js:

function otherStuff() { return ['a','b',undefined,'c'];
function doStuff() {
    var x = otherStuff();
    var z = parseInt(x[2]); // this will throw an error
}

console, after loading a html page that links page.js

> otherStuff();

I get no line number from the Error that is returned to me. When running it from the page (instead of the console) i would receive a line number and a stack trace.

1
  • what is the command? what version of chrome? (what is your mother's maiden name?) Commented Oct 18, 2011 at 19:40

2 Answers 2

11

Although verbose, this will print the stack trace of an interactive error in the Chrome JS console:

try { 
    throw new Error(); 
} catch (e) { 
    console.error(e.stack); 
}

Unfortunately this won't work if a non-Error object is thrown.

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

3 Comments

This works quite well. The data is formatted differently but all there (most important to me is line number). If a non Error object is thrown my stacktrace information will be lost to me, correct? Since we are retrieving it from the Error object in this case
Yes, same for any custom error object that isn't based on JS's standard Error objects. I tried setting a breakpoint in the catch clause when throwing a string, trying to see if Chrome creates some sort of proper exception object internally one could use – no such luck I'm afraid.
Just a note: MobileSafari does not appear to provide a "stack" property to its errors.
2

You have an error in your code.

You are missing a closing brace:

function otherStuff() { return ['a','b',undefined,'c']; //} where am i?
function doStuff() {
    var x = otherStuff();
    var z = parseInt(x[2]); // this will throw an error
}

Side point:

parseInt(undefined) does not throw an error. case in point: http://jsfiddle.net/maniator/Zequj/2/

Comments

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.