'Restart program if error encountered'

36 visualizaciones (últimos 30 días)
ANUBHAV SINHA
ANUBHAV SINHA el 10 de Abr. de 2012
Hi, Using the database toolbox, I query a ODBC database continuously by running an infinite loop for hours and use the real-time data of a experimental hardware. However, sometimes it happens that there is some 'bad data' getting stored in the database (such as wrong date format or the occurence of incomprehensible values of some real-time variable). As a result, the MATLAB program gives error and stops since it cannot understand the format of absurd 'bad data' enteries.
Hence, if an error is encountered, I want to automatically restart my program, reconnect to the database and continue (since I cannot sit at the computer 24x7 to restart the program again!). Also I know that my code is correct since it runs for hours flawlessly and it is simply a temporary instance of bad data that makes the program stop.
Can anyone suggest how I may 'restart automatically if an error is encountered'?

Respuestas (3)

Jan
Jan el 10 de Abr. de 2012
This seems like a task for try catch.
The fact, that a program runs for hours is not a sufficient argument the correctness. Better use the debugger to find out, why the program fails.
[EDITED] Explicit example for try catch :
for i = 1:5
try
yourProgramHere;
break; % Break out of the i-loop on success
catch ME
disp(ME);
fprintf('Retrying...\n');
end
end
  2 comentarios
Geoff
Geoff el 12 de Abr. de 2012
It's funny how we have such an ingrained habit of using 'i' as a loop variable. =P I'd love Google Code to run some stats over every instance of 'for' and show us the ratio of 'i' usage versus anything else.
Jan
Jan el 12 de Abr. de 2012
"i" and "j" are standard counter variables used in computer languages and on paper. I think pre-defining "i" *and* "j" as imaginary units was a mistake during the design phase of Matlab. While "1i" is clear, "1*i" is misleading.
I have seen one single case of using "i" incorrectly in the code posted in differen Matlab forums, and this case contained 8 bugs in 7 lines. In opposite to this, the symbols "max", "sum" and "pi" caused much more collisions. Therefore I stay at "for i", although the documentation suggest to avoid this. Sorry!

Iniciar sesión para comentar.


ANUBHAV SINHA
ANUBHAV SINHA el 10 de Abr. de 2012
I understand what you saying. It may be that you are correct too.
However, the hardware does 'mess up' at times in sending a garbage value. Hence, my question focused on forcing the MATLAB to try an extra attempt before it gives up at all.
It would be really helpful if you could suggest any clever idea to make MATLAB restart the program for (say) 5 times before MATLAB hangs its boots since it may be a temporary glitch in database.
Anubhav

Geoff
Geoff el 10 de Abr. de 2012
Sorry, but I'm gonna be your Mum here! The usual approach is not for your program to die horribly and have to be restarted, but to expect and handle bad data explicitly.
Another good practise is to not store bad values in your database in the first place. Surely your hardware is not the one responsible for INSERTs.
At the worst, you could wrap the offending code section in a try/catch and then continue unimpeded. But I am personally biased towards code that doesn't needlessly generate exceptions.
Don't put bad data in your database, or if that's unavoidable, make your code recognise and either fix or ignore the bad data that comes back out.
It's easy to check your date formats with a regular expression, and it's easy to test that a value is inside a specific range, or indeed test that it is a value at all.
  2 comentarios
ANUBHAV SINHA
ANUBHAV SINHA el 10 de Abr. de 2012
Fine, so if I choose to use try/catch, is the following the correct way to implement it inspite of error?:
count=1
while(count==1)
try
Main_function()
catch
Main_function()
throw
end
end
So the above code would ensure that whatever error may come, the Main_function() is re-run again? I am novice to this try-catch concept. Any advice?
I admit the above approach seems crude.
Geoff
Geoff el 12 de Abr. de 2012
No that's not right. Use Jan Simon's answer, and substitute 'Main_function()' for 'yourProgramHere'. If you want the loop to be infinite, then replace the 'for i = 1:5' part with 'while 1'. You want a loop that does something once, but restarts indefinitely if there is an error. I might also put a 'pause(1)' inside the catch just in case your function for whatever reason keeps exiting immediately.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by