How do I force the next loop iteration if error occurs within the loop?

조회 수: 534 (최근 30일)
Peta
Peta 2015년 6월 18일
댓글: Kyle Johnson 2018년 9월 6일
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.

채택된 답변

Jan
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
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
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
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".
  댓글 수: 1
Peta
Peta 2015년 6월 18일
Aaah! My bad, I totally misunderstood the meaning of continue and assumed it meant something else without looking in the documentation! Thanks for clearing that up!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by