# Can you find out the cause of the debugging in the program (exe) state?

**URL:** <https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898>\
**Category:** Coding Questions\
**Created:** [September 21, 2022, 5:06am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898 "2022-09-21T05:06:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 21, 2022, 5:06am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/1 "2022-09-21T05:06:13Z")

</div>

![fig](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/4/f4f4160c349c33650420312789fcc9b430b7c660.png)

Can you find out the cause of the debugging in the program (exe) state?

1. For example, in the case of processing (run), a debugging log file can be obtained.

2. In the case of processing (exe), is there a way to get the debugging log file?

If it is a processing (exe) file, the cause is unknown even if the program stops. I want to know why the program stopped, what should I do in this case?

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [September 21, 2022, 7:39am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/2 "2022-09-21T07:39:39Z")

</div>

You can try using printStream.

In your setup use

```auto
try{
PrintStream ps=new PrintStream("FileName");
System.setOut(ps);
System.setErr(ps);
}
catch(Exception e){}

```

This way the sketch will rerout the console to a file.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [September 21, 2022, 7:52am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/3 "2022-09-21T07:52:54Z")

</div>

Hi @GWAK,

Just looked, but it seems that the stderr/stdout not writing to a console or log by default, but if you have access to the source and are able to build the exe again, you can apply a log by yourself …

The Program below redirects the stderr and stdout to the files console.err and console.out, which would be created in the same directory as the executable at start (at least if you dblclick in Explorer).

Cheers  
— mnse

ie:

```auto
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.swing.JOptionPane;

int lastSec = -1;
void redirectConsole() {
  try {
    System.setErr(new PrintStream(new FileOutputStream(new File(sketchPath() + "/console.err"))));
    System.setOut(new PrintStream(new FileOutputStream(new File(sketchPath() + "/console.out"))));
  } 
  catch (Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
  }
}

void settings() {
  size(500, 500);
  redirectConsole();
}

void setup() {
  textAlign(CENTER, CENTER);
  textSize(50);
}

void draw() {
  background(0);
  fill(255);
  int m = int(millis()/1000); 
  text(m+"", width/2, height/2);
  if (lastSec != m) {

    // writes to console.err
    System.err.println("ERR: " + m);
    // writes to console.out
    System.out.println("OUT: " + m);
    println("println: " + m);

    lastSec = m;
  }
}

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 21, 2022, 11:29am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/4 "2022-09-21T11:29:21Z")

</div>

@NumericPrime@mnse

Thank you both very much for your kind replies.

However, I want to save the log where the error occurred.

For example, the source code is configured as follows.

```auto
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.swing.JOptionPane;

int lastSec = -1;
int test_array[] = {0,0,0,0,0};

void redirectConsole() {
  String str_name = str(month())+"_"+str(day())+"_"+str(hour())+"_"+str(minute())+"_";
  // println(sketchPath() + "/data/"+str_name+"console.err");
  try {
    System.setErr(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.err"))));
    // System.setOut(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.out"))));
  } 
  catch (Exception e) {    
    JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
  }
}

void settings() {
  size(500, 500);
  redirectConsole();
}

void setup() {
  textAlign(CENTER, CENTER);
  textSize(50);
}

void draw() {
  background(0);
  fill(255);
  int m = int(millis()/1000); 
  text(m+"", width/2, height/2);
  test_array[m]=m+1;
  
  if (lastSec != m) {
    System.err.println("ERR: " + m);
    lastSec = m;
  }  
  
}

```

 ![fig_1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/0/408713d4413894f7ca8008ea71d0e8d258920053.jpeg)

As in the above source code and picture  
Program hung with ‘ArrayIndexOutOfBoundsException: 5’.  
I hope that such a message will be saved as above.

Do you have any good way?

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [September 21, 2022, 11:32am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/5 "2022-09-21T11:32:13Z")

</div>

You can try to use try/catch take a look at this code:

```auto
void setup() {
}
void draw() {
  try {
    drawCode();
  }
  catch(Exception e) {
    e.printStackTrace();
    System.err.println("An error occurred... shutting down");
    exit();
  }
}
void drawCode() {
  if (random(0, 1)<0.2) throw new RuntimeException("Something went wrong");
}

```

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [September 21, 2022, 11:36am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/6 "2022-09-21T11:36:53Z")

</div>

To fix your issue specificly I would recommend using ArrayLists instead like this:

```auto
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.swing.JOptionPane;
import java.util.*;

int lastSec = -1;
List<Integer> test_array = new ArrayList<Integer>();

void redirectConsole() {
  String str_name = str(month())+"_"+str(day())+"_"+str(hour())+"_"+str(minute())+"_";
  // println(sketchPath() + "/data/"+str_name+"console.err");
  try {
    System.setErr(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.err"))));
    // System.setOut(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.out"))));
  } 
  catch (Exception e) {    
    JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
  }
}

void settings() {
  size(500, 500);
  redirectConsole();
}

void setup() {
  textAlign(CENTER, CENTER);
  textSize(50);
}

void draw() {
  background(0);
  fill(255);
  int m = int(millis()/1000); 
  text(m+"", width/2, height/2);
  while(m>=test_array.size()) test_array.add(m+1);
  test_array.set(m,m+1);
  if (lastSec != m) {
    System.err.println("ERR: " + m);
    lastSec = m;
  }  
  
}

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 21, 2022, 11:42am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/7 "2022-09-21T11:42:55Z")

</div>

@NumericPrime

You are a genius. Thanks for the kind explanation and reply.

```auto
void draw() {
  try {
    drawCode();
  }
  catch(Exception e) {
    e.printStackTrace();
    System.err.println("An error occurred... shutting down");
    exit();
  }
}

```

When using the code in this form, there may be a problem.  
Are there any disadvantages such as slowness when using it compared to not using it?

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [September 21, 2022, 11:46am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/8 "2022-09-21T11:46:36Z")

</div>

I just looked it up. Using try has only a minor cost. Catching an Exception however is more slow. However since you don’t need to catch them all the time you should not notice a dip in performance.

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 21, 2022, 11:48am UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/9 "2022-09-21T11:48:42Z")

</div>

@NumericPrime

I understand your meaning. thank you.

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [September 21, 2022, 12:06pm UTC](https://discourse.processing.org/t/can-you-find-out-the-cause-of-the-debugging-in-the-program-exe-state/38898/10 "2022-09-21T12:06:07Z")

</div>

I’d like to add one last thing:  
Another advantage of having a central try/catch block is that if you have an exception you are 100% sure won’t occurr you can just hand it to the central try catch block:  
For example if you used:

```auto
void drawCode() {
  boolean error=false;
  if(error) throw new IOException("testing");
}

```

Java will complain because you don’t handle IOExceptions. If you use

```auto
void drawCode() throws Exception{
  boolean error=false;
  if(error) throw new IOException("testing");
}

```

It won’t and since you catch it in draw.
