admin管理员组

文章数量:1323029

I want to create a loop that will continue until it reaches an error then continues on...

for (var m = 0; m != 'Error'; m++)

If I push the iterations too high, it will throw a "TypeError", which indicates that the limit has been reached. This for loop exists inside another loop which needs to continue and not crash the script, just discontinue the loop.

Thanks!

EDIT CODE FOLLOWS:

for (var i = 0; i < 100; i++) {
  var xml = fs.readFileSync('./Filename-' + (i + 100) + '.xml', 'utf-8');
  var level1 = bb(xml)
  for (var m = 0;; m++) {
    try {
     if (level1.data.level2[m].product.code == '7472558') {
        console.log(level1.data.level2[m].product.code);
        total++}
    }
    catch (e) {
     break;
    }
  }
console.log(total)
}

I want to create a loop that will continue until it reaches an error then continues on...

for (var m = 0; m != 'Error'; m++)

If I push the iterations too high, it will throw a "TypeError", which indicates that the limit has been reached. This for loop exists inside another loop which needs to continue and not crash the script, just discontinue the loop.

Thanks!

EDIT CODE FOLLOWS:

for (var i = 0; i < 100; i++) {
  var xml = fs.readFileSync('./Filename-' + (i + 100) + '.xml', 'utf-8');
  var level1 = bb(xml)
  for (var m = 0;; m++) {
    try {
     if (level1.data.level2[m].product.code == '7472558') {
        console.log(level1.data.level2[m].product.code);
        total++}
    }
    catch (e) {
     break;
    }
  }
console.log(total)
}
Share Improve this question edited Apr 10, 2016 at 2:02 2xshot asked Apr 10, 2016 at 1:14 2xshot2xshot 111 gold badge1 silver badge3 bronze badges 5
  • 1 m will never equal an error, just some integer. Do you set m anywhere in the body of the for loop? – Matthew Herbst Commented Apr 10, 2016 at 1:17
  • Use try..catch to handle a thrown error. Insert it between the two loops. – Jonathan Lonowski Commented Apr 10, 2016 at 1:19
  • Yes, "m" represents a numbered iteration, I do not know the upper bounds, and there should not be a numbered upper-bound, as "m" could represent hundreds – 2xshot Commented Apr 10, 2016 at 1:24
  • Why use the error to terminate the inner loop instead of looping while m < level1.data.level2.length? – andyk Commented Apr 10, 2016 at 2:12
  • @andyk Ahhh... you're right that's a better fix. totally forgot about the .length, it was staring right at me the whole time! Thanks! – 2xshot Commented Apr 10, 2016 at 2:23
Add a ment  | 

2 Answers 2

Reset to default 5

You can wrap the code that throws the error in a try and then break in the catch block:

for (var m = 0;; m++) {
  try {
    // code that throws the error
  } catch (e) {
    // exit the loop
    break; 
  }
}

(Note that the loop above will only terminate if the code throws an error.)

while( someOuterLoopCondition ) {

    var m = 0;
    var innerLoopOK = true;
    while( innerLoopOK ) {

        try {

            // your original inner-loop body goes here

        } catch e {
            innerLoopOK = false;
        }

        m++;
    }
}

Or:

while( someOuterLoopCondition ) {
    for( var m = 0; m < upperBound; m++ ) {
        try {
            // your original inner-loop body goes here
        } catch e {
            break; // breaks inner-loop only
        }
    }
}

本文标签: Javascript for loop until errorStack Overflow