Getting stack trace for error while running code from console in Chrome

I call the function from the console, but when it throws an exception, I do not get the stack trace, as if the code was executing normally.

Is there a way to modify my command (possibly with try / catch) so that it provides me with 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 } 

after loading the html page that links page.js

 > otherStuff(); 

I do not get the line number from Error, which is returned to me. When you start it from the page (instead of the console), I get the line number and the stack trace.

+6
source share
2 answers

Despite the verbosity, this will print a stack trace of the interactive error in the Chrome JS console:

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

Unfortunately, this will not work if the Error object is called.

+11
source

You have a bug in your code.

You are missing the closing parenthesis:

 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) makes no mistake. example: http://jsfiddle.net/maniator/Zequj/2/

+2
source

Source: https://habr.com/ru/post/899552/


All Articles