Why no error info as the catch statement's output for iterator2?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>th1 node</li>
        <li>th2 node</li>
        <li>th3 node</li>
        <li>th4 node</li>
        <li>th5 node</li>
    </ul>
    <script charset="utf-8" type="text/javascript" >
    var documentNode=document.body;
    const iterator1 = document.evaluate('//li', documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
    try {
      let thisNode = iterator1.iterateNext();
      while (thisNode) {
        console.log(thisNode.textContent);
        thisNode = iterator1.iterateNext();
      }
    }
    catch(e) {
      console.log(`Error: Document tree modified during iteration ${e}`);
    }
    const iterator2 = document.evaluate('//p', documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
    try {
      let thisNode = iterator2.iterateNext();
      while (thisNode) {
        console.log(thisNode.textContent);
        thisNode = iterator2.iterateNext();
      }
    }
    catch(e) {
      console.log(`Error: Document tree modified during iteration ${e}`);
    }
    </script>
</body>
</html>

For the above html,i can get log info in console.

th1 node nodes.html:21:17
th2 node nodes.html:21:17
th3 node nodes.html:21:17
th4 node nodes.html:21:17
th5 node nodes.html:21:17

Why no Error info for iterator2 ? No p element in the html file at all,why did the catch statement not execute for iterator2?

You have 5 li elements in your body, therefore you see console.log(thisNode.textContent); called 5x.

You have 0 p elements, therefore console.log(thisNode.textContent) in the second while block is called 0x.

1 Like