zoukankan      html  css  js  c++  java
  • 温故而知新 js 的错误处理机制

    // 在函数块的try中return,会直接成为函数的return值
    function test() {
        try {
            alrt(123)
            return 'success'
        } catch(err) {
            return 'fail'
        }
    }
    var result = test()
    console.log(result); // fail
    
    /**
     * 浏览器全局错误的处理
     * web 浏览器,所有未捕获的错误向上冒泡,最终由window.onerror 这一最高层级的事件函数处理。
     * 请注意,在web浏览器的控制台报错是无法被捕获的
     */
    window.onerror = function (msg, url, line, col) {
        console.log(msg, url, line, col);
        return true; // 告诉浏览器错误已被处理,没必要展示给用户
    }
    
    /**
     * node.js 的全局错误处理
     * process 对象会触发unCaughtException 事件。
     */
    process.on('unCaughtException', function (err) {
        console.log(err);
    })
    
    /**
     * domain 运行代码以及错误捕捉
     */
    var domain = require('domain').create();
    domain.on('error', function (err) {
        console.log(err);
    });
    
    /**
     * 该示例的基本思想是,也许会引发错误的代码可以放在这里运行
     * 若函数调用的代码引发错误,将触发该domain的错误事件。通过监听error事件可以做出恰当的处理
     */
    domain.run(function () {
        /* some code that might throw an error */
    })
  • 相关阅读:
    c# winform treelistview的使用(treegridview)
    基于Windows服务的聊天程序
    .Net Core集成Office Web Apps(二)
    .Net Core集成Office Web Apps(一)
    .Net页面局部更新的思考
    C#下载歌词文件
    jquery导航栏
    Select2下拉框总结
    数位dp入门(内容一样,新版格式)
    我的emacs简易配置
  • 原文地址:https://www.cnblogs.com/CyLee/p/9083158.html
Copyright © 2011-2022 走看看