# How to stop program immediately?

**URL:** https://discourse.processing.org/t/how-to-stop-program-immediately/36156
**Category:** Electronics (Arduino, etc.)
**Created:** [April 6, 2022, 9:25am UTC](https://discourse.processing.org/t/how-to-stop-program-immediately/36156 "2022-04-06T09:25:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Smajdalf](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Smajdalf](https://discourse.processing.org/u/Smajdalf)
#### Post date: [April 6, 2022, 9:25am UTC](https://discourse.processing.org/t/how-to-stop-program-immediately/36156/1 "2022-04-06T09:25:50Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 6, 2022, 9:41am UTC](https://discourse.processing.org/t/how-to-stop-program-immediately/36156/2 "2022-04-06T09:41:16Z")

</div>

Also with void you can use `return`.  
You need:

```auto
exit();
return;

```

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

---

<div class="post-metadata">

### Author: ![Smajdalf](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Smajdalf](https://discourse.processing.org/u/Smajdalf)
#### Post date: [April 6, 2022, 10:24am UTC](https://discourse.processing.org/t/how-to-stop-program-immediately/36156/3 "2022-04-06T10:24:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)
#### Post date: [April 6, 2022, 12:06pm UTC](https://discourse.processing.org/t/how-to-stop-program-immediately/36156/4 "2022-04-06T12:06:53Z")

</div>

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