'Restart program if error encountered'
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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'?
0 Kommentare
Antworten (3)
Jan
am 10 Apr. 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 Kommentare
Geoff
am 12 Apr. 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
am 12 Apr. 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!
Geoff
am 10 Apr. 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 Kommentare
Geoff
am 12 Apr. 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.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!