So a program is running, and disaster happens (one flavor or another)
I have seen code with
while(1);
for example in many example regarding
if (myfile)
returns zero. Those get followed by a while(1)
Which I think keeps the program running indefinitely.
But I also see that I could use
exit(0);
to fall out of the program completely.
What are the advantages/dis and resultant actions of those choices?
Any other than dependent hardware falling into unknown states?
Exit drops execution to the O/S. The Arduino doesn’t have one, so it’s of questionable value. Might be of academic interest to see what the compiler generates for it though.
Embedded controllers should NEVER just die. Like a battleship, it should keep firing until completely dead. No sensor? Flag the values and keep flying. No SD? Mention no data will be saved and still show values, etc. Going DOA is flat wrong and will get your hardware replaced. At least keep a heartbeat LED blinking with a fault code, "... --- ..." always works.
I've tested on a Nano (AVR / ATmega328): exit(0) will halt the chip completely and not allow any further interrupts to happen which causes serial communication to stop. while(1); halts execution indefinitely but interrupts are still handled and serial communication continues.
void setup()
{
Serial.begin(9600);
Serial.println("Some error happened, bye..");
while(1); //This will allow the message to arrive in the serial monitor
//exit(0); //This will not allow the message to arrive
}