フィルターのクリア

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

3 ビュー (過去 30 日間)
farzad
farzad 2019 年 6 月 27 日
コメント済み: Jan 2019 年 7 月 1 日
Hi all
is there a line to indicate the main code between the try catch loop to avoide rewriting it ?
  5 件のコメント
farzad
farzad 2019 年 6 月 28 日
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
Jan
Jan 2019 年 6 月 28 日
@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

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 6 月 27 日
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 件のコメント
farzad
farzad 2019 年 7 月 1 日
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 ?
Jan
Jan 2019 年 7 月 1 日
Just take a look in the code of my answer:
catch ME
fprintf('Error: %s\n', ME.message);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by