作家
登录

深入理解JavaScript错误和堆栈追踪

作者: 来源: 2017-04-25 17:10:21 阅读 我要评论

  • }).then(function() { 
  •     console.log('I am an error.'); 
  • }).catch(function(err) { 
  •     if (err instanceof Error) { 
  •         console.log('The promise was rejected with an error.'); 
  •         console.log('Error Message: ' + err.message); 
  •     } 
  • }); 
  • 把持客栈跟踪

    膳绫擎烦琐了那么多,压轴的重头戏来了,那就是若何把持客栈跟踪。

    本章专门针对那些像NodeJS支Error.captureStackTrace的情况。

    Error.captureStackTrace函数接收一个object作为第一个参数,第二个参数是可选的,接收一个函数。capture stack trace 捕获当前客栈跟踪,并在目标对象中创建一个stack属性来存储它。如不雅供给了第二个参数,则传递的函数将被视为调用客栈的终点,是以客栈跟踪将仅显示调用该函数之前产生的调用。

    让我们用例子来解释这一点。起首,我们将捕获当前客栈跟踪并将其存储在公共对象中。

    1. const myObj = {}; 
    2.  
    3. function c() { 
    4.  
    5. function b() { 
    6.     // Here we will store the current stack trace into myObj 
    7.     Error.captureStackTrace(myObj); 
    8.     c(); 
    9.  
    10. function a() { 
    11.     b(); 
    12.  
    13. // First we will call these functions 
    14. a(); 
    15.  
    16. // Now let's see what is the stack trace stored into myObj.stack 
    17. console.log(myObj.stack); 
    18.  
    19. // This will print the following stack to the console: 
    20. //    at b (repl:3:7) <-- Since it was called inside B, the B call is the last entry in the stack 
    21. //    at a (repl:2:1) 
    22. //    at repl:1:1 <-- Node internals below this line 
    23. //    at realRunInThisContextScript (vm.js:22:35) 
    24. //    at sigintHandlersWrap (vm.js:98:12) 
    25. //    at ContextifyScript.Script.runInThisContext (vm.js:24:12) 
    26. //    at REPLServer.defaultEval (repl.js:313:29) 
    27. //    at bound (domain.js:280:14) 
    28. //    at

        推荐阅读

        深度卷积神经网络在目标检测中的进展

      近些年来,深度卷积神经收集(DCNN)在图像分类和辨认上取得了很明显的进步。回想大年夜 2014 到 2016 这两年多的时光,先后出现出了 R-CNN,Fast R-CNN, Faster R-CNN, ION, HyperNet, SD>>>详细阅读


      本文标题:深入理解JavaScript错误和堆栈追踪

      地址:http://www.17bianji.com/lsqh/34953.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)