如何判断Javascript错误是否会阻止'?

时间:2017-06-18 11:05:15

标签: javascript jquery

是否存在阻止与非阻止Javascript错误之类的事情?

我的意思是我看到我的网站报告了一些错误,但所涉及的过程仍然成功完成(据我所知)。这些错误包括: TypeError:undefined不是对象(评估' e.nodeName.toLowerCase') TypeError:e.nodeName未定义 (这些总是来自我的常规JQuery库文件)

这就是我所说的非阻塞'错误,因为涉及的脚本似乎成功完成。

如果存在阻止v非阻塞这样的事情,那么它的正确术语是什么,以及如何区分2之间的区别(例如,我可以优先考虑阻塞错误)?

由于

2 个答案:

答案 0 :(得分:2)

每当

throw Error;

异常会使功能堆栈崩溃。如果它到达它的末尾,它会抛出未捕获的(=意外)异常。这会使整个程序暂停,因为它不太可能正常工作。

但是它可以 catched (=预期的例外):

try{
 someerrorproducingfunc();
 //continue after success
}catch(e){
 //continue after error
 console.error(e);
}
//continue after error+success

因此代码继续进行(在成功之后继续部分)。但是,如果您使用 console.error ,它会记录看起来像一个错误。

ressource:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/throw

请注意,错误只是js中的对象构造函数:

new Error("test");

throw 抛出异常 ...

答案 1 :(得分:0)

没有。所有未捕获的错误都是阻塞的,它们将结束整个调用堆栈的执行。但是,在各种操作之后发生的错误将不会恢复到以前的状态。进行AJAX调用,然后出现未定义的错误将不会取消调用,例如。演示:

(function () {
    "use strict";
    let a  = 5;
    
    console.log(a); // 5
    
    setTimeout(function () {
        console.log(a); // 10
        // will execute because it is a fresh stack
        // the variable a is 10 despite and not 12 because it happened after
    }, 250);
    
    const withError2 = function () {
        a = 10;
        a.x.z = 2; // Error
        a = 12; // never executes because it comes after the error
    }
    
    withError2();
    console.log(a); // never executes because it comes after the error
}());

相关问题