How not to repeat the whole code after the try catch loop ?

Hi all
is there a line to indicate the main code between the try catch loop to avoide rewriting it ?

5 Kommentare

Jan
Jan am 27 Jun. 2019
Bearbeitet: Jan am 27 Jun. 2019
What is a try catch loop? try catch means branching in case of errors, but there is no loop. What is "the main code" and what might "between the loop" mean? For "between" there need to be at least 2 limits, but "between the loop" is only one.
"Between the bag there sat a man."
Please post some code, which demonstrates, what you are asking for. Prefer to edit the question instead of adding this information in a comment.
Please read this answer and you will see how the structure is suggested
for j = 1:10
try
... your code...
catch
warning('found error in %dth file', j);
continue
end
... your code, if "try" codes succeed
end
What does "how the structure is suggested" mean? If you are interested in assistance to solve your problem, it is useful to answer the questions for clarifications.
The answer I linked above, shows that I should write my code twice : as you see the lines in green
if my code is too long , every time I have to apply a modification in the code, I should do it two times.
I can't be more clear than this
@farzad: You can be more clear: Simply explain, which problem you want to solve.
In this code:
for j = 1:10
try
... your code...
catch
warning('found error in %dth file', j);
continue
end
end
you do not have to write any code twice. The first comment (green line) is a placeholder for your code, and the second comment is some other code, which is executed only, if the first one ran without an error. An alternative version of the above code:
for j = 1:10
try
... your code...
... your code, if the above statement succeed
catch
warning('found error in %dth file', j);
end
end
So do you see now, that you do not write anything twice here?
You still did not mention, which problem you want to solve and you assume that anything must be written twice. My questions for clarifications are not answered yet.
You are not a newcomer in this forum, but please let me ask you to read this again: How to ask a good question

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 27 Jun. 2019
A bold guess, which contains at least the terms "loop" and "try catch" and "avoid rewriting it":
ready = false;
while ~ready
try
executeTheCode;
ready = true;
catch ME
fprintf('%s\ntrying it again...\n', ME.message);
end
end

11 Kommentare

Thank you
have you seen this answer ? is your code an improvement of this one ?
Jan
Jan am 27 Jun. 2019
Bearbeitet: Jan am 27 Jun. 2019
The code is not an improvement, but it does something else. As soon as you take the time to answer my questions for clarifications and explain, what you need, I can tell you, which of these two different methods is matching.
for j = 1:10
try
... your code...
catch
warning('found error in %dth file', j);
continue
end
... your code, if "try" codes succeed
end
This code calls te function "your code" 10 times and proceeds to the next iteration in case of an error.
My code repeats the call of "executeTheCode" until it is successful.
Currently all I can do is guessing, because I do not understand your question. Neither "a line to indicate the main code between the try catch loop to avoide rewriting it" nor "you will see how the structure is suggested" reveals, what you want to achieve.
since the green line : yourcode is repeated twice , every time I need a correction, I should do it twice or copy and paste
I need to avoid it, how is that possible ?
Stephen23
Stephen23 am 28 Jun. 2019
Bearbeitet: Stephen23 am 28 Jun. 2019
"I need to avoid it, how is that possible ?"
By using Jan's answer which was posted 19 hours ago.
It only calls your code once, exactly where the rather explict executeTheCode is written.
"yourcode is repeated twice"
Nope. What you are commenting on is not Jan's answer, but some code that was copied from another answer which you linked to. That code has nothing to do with Jan's answer, nor does it work in the same way, or even do the same thing.
What's does the code I have linked to do?
Stephen23
Stephen23 am 28 Jun. 2019
Bearbeitet: Stephen23 am 28 Jun. 2019
"What's does the code I have linked to do?"
Jan already explained that : "This code calls the function "your code" 10 times and proceeds to the next iteration in case of an error." The part after the try-catch will only run if there is no error on that iteration.
It has nothing to do with Jan's answer, nor does it really help you with your original question.
"This code calls the function "your code" 10 times and proceeds to the next iteration in case of an error."
Actually this is what I need to do : I need to run all the iterations possible except the one that gives error.
the only thing I do not want to do is : not to put the same code twice.
Why ? because I have like 10 sets of runs, that read from some excel files and I do the same type of calculation on each of them. while some of these files might be corrupted, I want the code continue with the files that have not corurpted numbers inside them ( I don't know the file problem type so I don't want to use if) and then later after the whole analysis is done I will deal with the corrupted ones
Stephen23
Stephen23 am 28 Jun. 2019
Bearbeitet: Stephen23 am 28 Jun. 2019
@farzad: so just use a simple loop, with a try-catch :
for k = 1:10
try
... your code...
catch
warning('found error in %dth file',k);
end
end
Amazing ! thank you
guys, just one morething : how can I print the Error Text for each case the error is catched ?
I mean additionally to this
warning('found error in %dth file',k);
what can I write to print the Error text ?
Just take a look in the code of my answer:
catch ME
fprintf('Error: %s\n', ME.message);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 27 Jun. 2019

Kommentiert:

Jan
am 1 Jul. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by