필터 지우기
필터 지우기

How to put script on pause instead of quitting when encountering error

조회 수: 6 (최근 30일)
I am trying annotate several genomes. So I made a script that first predicts open reading frames and then BLAST-searches those against remote protein database. But I have a problem each time there is smth wrong with connection. In this case the script just quits because of the 'lost connection' error. Is there any option that if the script faces an error, it pauses the whole thing and continues shortly after rather than quitting completely?

채택된 답변

Guillaume
Guillaume 2015년 3월 10일
Use a try catch block inside a while loop to catch the error and attempt the reconnection. Something like
notconnected = true;
while notconnected
try
connect(); %replace with whatever code you use to connect
%if connect throws an error, the following won't be executed and
%notconnected will stay true
notconnected = false;
catch
%connection failed
pause(2); %wait a little while before trying again
end
end %unless connect succeeded and notconnected is alse try to connect again

추가 답변 (1개)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2015년 3월 10일
편집: Giorgos Papakonstantinou 2015년 3월 10일
Ekaterina, as far as I know you, could enclose the part of the code, which tries to establish a connection within a try, catch statement.
For example lets say you want to establish a connection to the Matlab central answers.
What you could do is this:
ii=0;
while ii<10
try
A = urlread('http://www.mathworks.com/matlabcentral/ans/');
end
ii = ii+1;
end
Here I have intentionally given a false url and therefore the variable A will not exist. Matlab would complain if I had not enclosed the urlread command in a try, catch statement and would have thrown an error.
Error using urlreadwrite (line 88)
The server did not find a resource to match this request.
Error in urlread (line 36)
[s,status] = urlreadwrite(mfilename,catchErrors,url,varargin{:});
You can also modify your while condition for example:
while isempty(A)
assuming that prior the while loop you have issued:
A = [];

카테고리

Help CenterFile Exchange에서 Manage Products에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by