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 */
    })
  • 相关阅读:
    python之Selenium
    Python常用集锦(upgrading...)
    豆瓣爬虫
    poj 2299 Ultra-QuickSort(求逆序对)
    nyoj117 求逆序数
    codeforces 644A Parliament of Berland
    codeforces 659A Round House
    poj 3264 Balanced Lineup(RMQ裸题)
    nyoj 119 士兵杀敌(三)(RMQ)
    hdu 5655 CA Loves Stick
  • 原文地址:https://www.cnblogs.com/CyLee/p/9083158.html
Copyright © 2011-2022 走看看