MATLAB: How to force the next loop iteration if error occurs within the loop

errorfor loopwebread

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.

Best Answer

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.