• 请不要在回答技术问题时复制粘贴 AI 生成的内容
whereabouts
V2EX  ›  程序员

网页前端 Exception 未捕捉处理后 js 就停止执行了 有办法统一捕捉并忽略吗

  •  
  •   whereabouts · Aug 30, 2018 · 1621 views
    This topic created in 2906 days ago, the information mentioned may be changed or developed.
    发生在 jquery 的 ajax 或者普通 JavaScript 代码写错的时候

    $.ajax({
    type: 'POST',
    ...参数略过,
    success: function(res){
    },
    failure: function(err){
    },
    });

    JavaScript 最后面设置了两个捕捉错误并忽略的代码( StackOverflow 上搜的):
    window.onerror = function(error){
    console.log(error)
    }
    window.addEventListener('error', function(e) {
    console.log(e)
    });

    使用时当 ajax 目标网址宕机(端口关闭)时,chrome 的 console 报错并且 JavaScript 停止执行,应该是没有处理 Exception 导致的。
    个人目前的理解是$.ajax 的 failure 对用函数除了网络连接失败之类的错误,而末尾两端代码捕获并处理未显式 Handle 的异常。
    但是无论是 ajax 失败还是其它 JavaScript 代码写错而导致的异常都会使 JavaScript 挂掉,请指点一下自己是哪里理解错了?
    3 replies    2018-08-30 15:21:15 +08:00
    xxx749
        1
    xxx749  
       Aug 30, 2018 via Android
    你只是捕获了,并没有做处理,在代码底部加上 return false,具体参考高程 3,506 页
    molvqingtai
        2
    molvqingtai  
       Aug 30, 2018
    try{

    } cache {

    }
    doublleft
        3
    doublleft  
       Aug 30, 2018
    @molvqingtai

    try / cache 是啥
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1048 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:05 · PVG 07:05 · LAX 16:05 · JFK 19:05
    ♥ Do have faith in what you're doing.