0

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)
}
2xshot
  • 1
  • 1
  • 1
  • 2
  • 1
    `m` will never equal an error, just some integer. Do you set `m` anywhere in the body of the `for` loop? – Matthew Herbst Apr 10 '16 at 01:17
  • Use [`try..catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) to handle a thrown error. Insert it between the two loops. – Jonathan Lonowski Apr 10 '16 at 01: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 Apr 10 '16 at 01:24
  • Why use the error to terminate the inner loop instead of looping while `m < level1.data.level2.length`? – andyk Apr 10 '16 at 02: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 Apr 10 '16 at 02:23

2 Answers2

4

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.)

andyk
  • 1,628
  • 15
  • 12
  • Still getting a "SyntaxError: Missing catch or finally after try" using notepadd++ tracing out all the {} everything seems ok....? – 2xshot Apr 10 '16 at 01:42
  • @2xshot hard to tell what could be causing the syntax error without seeing your updated code. My guess would be that it's a missing or misplaced brace. – andyk Apr 10 '16 at 01:46
  • running in node.js, I think its exiting too early and not proceeding to the catch. – 2xshot Apr 10 '16 at 01:55
  • If you update your question to contain the body of the loop I bet someone will be able to spot the issue. But without seeing the code it's hard to say what's going on – andyk Apr 10 '16 at 02:02
0
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
        }
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Something important to note. If you want to break out of the outer loop in this case and not just the inner loop you could give the outer loop a label and then use "break label". See here for an example: http://stackoverflow.com/questions/1564818/how-to-break-nested-loops-in-javascript – Robert McMahan Apr 10 '16 at 01:28