【问题标题】:Chrome: Print exception details to consoleChrome:将异常详细信息打印到控制台
【发布时间】:2013-10-02 21:17:05
【问题描述】:

如何从我的代码中打印 chrome devtools 中的异常堆栈跟踪?

我尝试了以下方法:

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

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

但这会产生以下结果:

 Exception thrown TypeError {}

如果我展开它旁边的箭头,它会将我指向执行 console.error() 调用的行,因此我看不到原始错误实际发生的位置。

在控制台输出中包含原始错误信息(包括消息和完整堆栈跟踪到错误发生的确切位置)的最佳方式是什么?

【问题讨论】:

    标签: javascript google-chrome google-chrome-devtools


    【解决方案1】:

    您可以将错误输出为对象

    console.error("%O", e)
    

    Using string substitutions

    【讨论】:

      【解决方案2】:

      对象Error 有一个属性stack。打印出来。

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

      请注意stack 属性未标准化,仅用于基于 V8 的浏览器 + IE。 Firefox 使用不同的约定。

      【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 2015-09-29
      • 2012-03-23
      • 1970-01-01
      相关资源
      最近更新 更多