How do I force the next loop iteration if error occurs within the loop?
322 次查看(过去 30 天)
显示 更早的评论
I have a loop that is supposed to run a very long time that starts with a webread command. Sometimes there is something wrong with the page it’s trying to read which causes the script to stop and display the error. From what I understand you can put the command within a “try” command to continue despite the error like this:
try
webread(‘www.something that could or could not generate an error.com’)
catch
continue
end
First of all; is that the correct way of writing it or would I need to specify anything more after writing “catch”? And can I modify this so that instead of continuing the script it restarts it from the next iteration in the loop? If I write:
for i=1:100
try
webread(‘www.something that could or could not generate an error.com’)
catch
end %I want this to cause the loop to jump up to the next value of i and restart the loop
end
end
Matlab won’t understand what I mean with my double end statement. Is there some other way of doing this? I want my script to always ignore errors and just try again with a new iteration.
0 个评论
采纳的回答
Jan
2015-6-18
编辑:Jan
2015-6-18
The end is not the correct method to jump to the next iteration. Actually the continue command would solve this, but in your case no command is enough already:
for i=1:100
try
webread('www.something that could or could not generate an error.com')
catch
% Nothing to do
end
end
When you read this code in some years, the empty catch branch might look confusing like you've forgotten to program this part. So insert a meaningful comment why this is empty.
In most cases it is useful to display a comment when a try fails, or at least store a message in a protocol or something like this. If e.g. all iterations fail, the code will not perform anything and the user might get doubts about what's going on. In addition there could be a programming error or the inputs of webread might be changed in a future version of Matlab. Then try catch will hide the error message an the user has no chance to detect this. Therefore it is a fair strategy to treat the catch branch as obligatory:
for i=1:100
try
webread('www.something that could or could not generate an error.com')
catch ME
fprintf('WEBREAD without success: %s\n', ME.message);
end
end
EDITED: The command "continue" will jump back to the surrounding for loop without processing the rest of the loop body:
for i=1:100
try
webread('www.something that could or could not generate an error.com')
catch ME
fprintf('WEBREAD without success: %s\n', ME.message);
continue; % Jump to next iteration of: for i
end
fprintf("WEBREAD worked successfully\n");
end
Now the success message will not appear in a case of an error. When there is no code behind the try-catch block, the continue has no effect, because there are no commands to be skipped.
Use continue carefully. In a bigger code this command can increase or reduce the readability. It might be "nicer" to move the success message insider the try branch. However, a descriptive comment is the best idea to state the purpose of continue clearly.
4 个评论
Pouya Jamali
2018-8-27
编辑:Pouya Jamali
2018-8-27
Nice! How can I get the code try the same loop again?! I have random Internet connection problems for which I want to wait for some time and then execute the same loop index again. If I simply continue to the next loop I will miss an item from my list.
I know I can do this using a while loop but just wondering if there is a workaround for for loops as well.
Kyle Johnson
2018-9-6
I achieved this by using a while loop instead of a for loop.
i = 1;
e = 0; % error counter
I = 100; % Total number of loops
while i<I
try
webread('www.something.com')
i = i+1;
e = 0; % reset error counter
catch
pause(.5) % pause to see if internet improves
e = e+1; % add an error
if e>20
keyboard % if you are just getting excessive
% errors, this will stop the loop
% and put you in debugger mode to
% try and take care of the problem
end
end
更多回答(1 个)
Walter Roberson
2015-6-18
It is recommended that you follow "catch" by the name of a variable to receive the error structure, such as
catch EX
"continue" already means "start the next execution of the loop without doing the rest of the body of the loop". It does not mean "ignore the error and keep going with the code". If you wanted to keep going with the code you would use
try
...
catch
end
with no "continue".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!