最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome: Print exception details to console - Stack Overflow

programmeradmin0浏览0评论

How do I print the stack trace of an Exception in the chrome devtools from my code?

I tried the following:

 function doSomething() { 
     undefined(); // This throws an exception
 }

 try {
      doSomething();
 } catch (e) {
      console.error("Exception thrown", e);
 }

But this yields the following result:

 Exception thrown TypeError {}

And if I expand the arrow next to it, it points me to the line where the console.error() call was made, so I don't get to see where the original error actually happened.

What would be the best way to include the original error information (including message and complete stack trace to the exact location where the error happened) in the console output?

How do I print the stack trace of an Exception in the chrome devtools from my code?

I tried the following:

 function doSomething() { 
     undefined(); // This throws an exception
 }

 try {
      doSomething();
 } catch (e) {
      console.error("Exception thrown", e);
 }

But this yields the following result:

 Exception thrown TypeError {}

And if I expand the arrow next to it, it points me to the line where the console.error() call was made, so I don't get to see where the original error actually happened.

What would be the best way to include the original error information (including message and complete stack trace to the exact location where the error happened) in the console output?

Share Improve this question asked Sep 26, 2013 at 18:40 urishurish 9,0338 gold badges56 silver badges75 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

Object Error has a property stack. Print it out.

console.error("Exception thrown", e.stack);

Please note that stack property is not standardized and it is only used by V8 based browsers + IE. Firefox uses different convention.

You can output the error as object

console.error("%O", e)

Using string substitutions

发布评论

评论列表(0)

  1. 暂无评论