How to use try/catch so that my program rerun until it correct ?

Dear Matlab users,
I need to process a large amount of data using parfor and I am running into a problem where my program can sometime success, sometime not success
parfor i = 1:100
MyProgram
end
Is there anyway to utilize the try/catch statement so that everytime when my progragram run into some kind of error, the worker will
1/ Retry untill success
2/ Put a warning about the iteration i and then abandon the current job to move on
Thank you very much

 Réponse acceptée

niters = 100;
status = logical(niters, 1);
message = cell(niters, 1);
parfor i = 1 : niters
blah blah blah
try
something
status(i) = true;
message{i} = [];
catch ME
status(i) = false;
message{i} = ME;
end
end

6 commentaires

Tuong Nguyen Minh
Tuong Nguyen Minh le 16 Juil 2020
Modifié(e) : Walter Roberson le 16 Juil 2020
Oh, So in this case if the worker encouter a error will it move on with the next job and output a message ?
Should I put MyProgram in the blah blah blah or something ?
(My guess is that I should put MyProgram in the something)
With that code, it will not display a message, but it will record information that you can examine after the entire parfor is finished.
You would replace blah blah blah with any "safe" initialization code that will not error. You would replace something with the code that might fail.
If you want to retry then,
maxretry = 42;
niters = 100;
retry_count = zeros(niters, 1);
parfor i = 1 : niters
blah blah initialization
while true
blah blah anything that needs to be reset every retry
try
something
break;
catch ME
retry_count(i) = retry_count(i) + 1;
if retry_count(i) > maxretry
break;
end
end
end
end
Wow this is more difficult than I expected but I appreciate the opportunity to learn your way of thinking.
However, for the second case (the code that support retry) what is the reason behind the usage of "break" in the try block ? I am quite worried that eventhough there is no error in "something" the program would break out of the parfor loop ?
Thank you very much !
The code assumes that after a while of retrying, that you are not going to succeed in a reasonable amount of time, and so it gives up. For example if you were randomly placing non-overlapping circles inside a square, after awhile you can be pretty sure that there just are no more empty gaps large enough and should give up.
The break is out of the "while true", not out of the parfor.
After the parfor you would check
failed = retry_count > maxretry;
and you would do whatever is appropriate to discount the results at locations that failed.
You do not have to keep track of retries; you could just let it retry indefinitely. That would make the code a bit more simple.
niters = 100;
parfor i = 1 : niters
blah blah initialization
while true
blah blah anything that needs to be reset every retry
try
something
break; %you did not fail, so you can stop retrying.
end
end
end
Perhaps it might be more clear to you as
niters = 100;
parfor i = 1 : niters
blah blah initialization
failed = true;
while failed
blah blah anything that needs to be reset every retry
try
something
failed = false;
end
end
end
Oh thank you very much, I did not know that this try catch command would even be that flexible. Your explanation is excellent and the code organization is very thoughful !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by