How to stop program immediately?

I want to write a simple program that check number of conditions and stop if some of them is wrong. I have everything in setup() and tried to use exit() for this but it does not work. I cannot return() either because setup() is void and it must be void. Is there a way to avoid using a lot of nested ifs? Example:

if (Serial.list().length>0) Arduino  = new Serial(this,Serial.list()[0],115200);
  else {
    println("Arduino not connected");
    exit();
  }
//do some stuff here
if (AnotherCheckFail==true) exit();
//another stuff

Also with void you can use return.
You need:

exit();
return;

exit() works not immediately but only at the end of draw().
Therefore combine it with return.

I was trying return(); and it did not work. Thanks!

1 Like

Just use System.exit(0); instead of using exit(); and your code will stop immediately.

2 Likes