在javascript中捕获循环错误?

时间:2017-04-06 06:47:06

标签: javascript mongodb

    for (var i = 0, len = IDs.length; i < len; i++) {

    entry = IDs[i];
    isAnyFailure = false;

    try{
    var id = db.getSiblingDB("door").jhi_user.findOne({"_id" : entry});
    } catch (err){

     failedIDs.push(entry);
     failedCount++;
     isAnyFailure = true;
    }

...
}

如果它捕获到任何错误,它会继续编码还是会进入下一次迭代?

1 个答案:

答案 0 :(得分:1)

循环继续尝试并捕获:

&#13;
&#13;
    for (var i = 0; i < 10; i++)
    {
      try
      {
        if(i == 6)
        {
          document.getElementById('noid').html("x"); //this will cause an exception as no such ID exists
        }
        console.log("In try Iteration :: "+i);
      }
      catch (err)
      {
        console.log("In catch Iteration :: "+i);
      }
    }
&#13;
&#13;
&#13;

try和catch的整个概念是在发生异常时继续执行,而不是突然停止程序/脚本。