Question for "try()"

Any Ideas, why this is not working?
grafik

Processing 3.5.4, Windows 10

Hello,

There is a statement here about the error that is returned:
https://processing.org/reference/loadStrings_.html

I ended up checking for a null.

If I tried to:

println(test)

I did get an expected:

NullPointerExeption

My exploration into this without a save.txt file to load:

String[] test;

void setup() 
	{
  size(200, 200);
  
  try
    {
    println("Try");
    test = loadStrings("save.txt");
    if (test == null)
      {
      println("null");
      //println(test); // This generates a NullPointerExeption !
      }
    }
  catch (Exception e)
    {
    println("Catch");  
    println(e);
    }	
  }

void draw() 
	{
  background(255);
	}

:)

1 Like

In Java most file access methods will throw exceptions including the IOException when things go wrong.

In the Processing source code, methods like loadStrings wraps includes all the Java file handling statements, these methods include try-catch statements to catch any exceptions thrown by Java, in that situation the Processing method will probably return null.

This is what happens inside loadStrings if a Java exception is thrown when executing the method it simply captures the exception and returns null. This means the user does not need to understand or use the try-catch syntax.

2 Likes

Can you please show me where in the source code?

I will probably look this up as well.
You may have more experience navigating in there.

:)

This links to the source code. Scroll done to line 7567 for the loadStrings(String) method which accepts a String representing the filename.

1 Like