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 setm
anywhere in the body of thefor
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
2 Answers
Reset to default 5You 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
版权声明:本文标题:Javascript for loop until error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742109903a2421198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论