Try catch on script for any error

1 回表示 (過去 30 日間)
arun Dhillon
arun Dhillon 2019 年 5 月 15 日
コメント済み: arun Dhillon 2019 年 5 月 15 日
Hello
I am having a script(myscript), that throws an error whenever there is any part of hardware missing, which is defined in different ways to be used inside of the script.
What I want to do is that whenever any error (maybe even some different error, that I may have not encountered before), whenever "any kind of error" occurs, the script shall retry running for one more time. What would be the best way to do this.
Is try catch the only option, and if yes please edify for how can I use it for 'myscript' to be executed one more time, when the error happens.
Thanks in advance
  2 件のコメント
Adam
Adam 2019 年 5 月 15 日
編集済み: Adam 2019 年 5 月 15 日
Turning it into a function with a sealed workspace would make it easier. But a try-catch would work with this in that case. Theoretically it would work with a script too, but having the workspace fully exposed is not ideal even though the second run should presumably just overwrite everything from the first run anyway.
try
myFunction( )
catch
myFunction( )
end
should be enough to do it though. So long as it isn't in a loop this will only re-run it once and then the error will just land on your command window as normal after the 2nd time.
Blanket error catching like this is not generally advisable though as even basic syntax errors will just result in it attempting to run a second time pointlessly before reporting your error, but I understand it can be difficult if you get a bunch of different hardware errors with identifiers you don't necessarily know.
arun Dhillon
arun Dhillon 2019 年 5 月 15 日
Adam
First of all, thanks a lot for your reply.
I have couple of questions for to ask you, firstly what do you mean by 'sealed workspace'?
Secondly, its fine in my case for the second run to overwrite, but what do you mean by 'fully exposed workspace'.
And finally the script is like calling for connections for bunch of different hardware and functions inside of itself. My question is, Ideally how would you convert that kind of script into a function, for which the input argument should be just the name of the script (like run button on the matlab) and the output argument is an m*5 matrix(pick).

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

採用された回答

Jan
Jan 2019 年 5 月 15 日
nRetry = 8;
for k = 1:nRetry
try
yourFunction();
break; % Exit from the loop on success
catch ME
fprintf('*** Failed: %s\n Retrying...\n', ME.message);
end
end
  1 件のコメント
arun Dhillon
arun Dhillon 2019 年 5 月 15 日
Thanks a lot

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by