Using the try{} catch() {}

Hi! I recently saw a function

try{

} catch() {

}

I am curently working on a “game of life” project. I use the int[][] grid = new int[w][h].
It is annoying if I have to use

if(i > 0) { grid[i-1][j] }

everytime I check (8 times)

how would i be able to use it?

int w = 20, h = 10;
float scl;
boolean grid[][] = new boolean[w][h];
boolean survive[][] = new boolean[w][h];


void setup() {
  size(800,400);
  scl = width/w;
  stroke(255);
}

void draw() {
  background(0);
  for(int i = 0; i < w; i++) { 
    for(int j = 0; j < h; j++) {
      fill( ((grid[i][j])? 255 : 0));
      rect(i*scl,j*scl,scl,scl);
    }
  }
}

void mousePressed() {
  int x = floor(map(mouseX,0,width,0,w)), y = floor(map(mouseY,0,height,0,h));
  grid[x][y] = !grid[x][y];
}

void check() {
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      int sum = 0;
      
      for(int a = -1; a < 3; a++) {
        for(int b = -1; b < 3; b++) {
          
          try {
            
          } catch {
            
          }
          
          
        }
      }
      
      if(sum == 3) {
        survive[i][j] = true;
      } else {
        survive[i][j] = false;
      }
    }
  }
}

instead of using try catch here, you could use a function checkIndex(i,j) which returns true or false


boolean checkIndex(i,j) {
    if(i>0&&i<5&&
    j>0&&j<5) 
    return true;
    else 
    return false; 
}
1 Like

I used this method in my previous GOL project. After I couldn’t find a way to use the try command I just reused the previous code.

I am not only learning the try command for this project, just like how I used arraylists for some projects where that wasn’t needed.

1 Like

so how to use the try{}?

Although understanding exception handling and being able to use the try...catch construct is good to know, it is more important to know when to use it.

The try...catch construct should be used to catch exceptional errors and iterating over an array should not cause exception because we always know the array or list size.

So by all means learn it, but don’t use it in a GoL project.

Game of Life is a fun interesting project which I first implemented several decades ago when I was fairly new to programming. It wasn’t very efficient but as my programming skills improved so has my algorithm to the point I have no need to perform any arrays bounds checking. It uses just two 2D arrays and a list. If you are familiar with bit operations it can be done with just one 2D array and a list. (you can see the algorithm in action here on my website)

I know this discussion is about try...catch so I won’t explain the algorithm here unless you want me to, :smiley:

6 Likes

This gives an error:

float a = 6 / 0;

println(a);


Here is with try catch


float a = 0;

try {
  a = 6 / 0;
} 
catch( Exception  e ) {
  println("catching...."); 
  println ("e says: "+e);
  e.printStackTrace();
}

println(a);

Chrisir

This cannot be overstated! Never* use exceptions and try/catch for program logic. Using try when the exception doesn’t happen is very efficient - triggering the catch, and particularly the actual creation of the exception, is really inefficient.

  • OK, as with everything, almost never - there are cases - this definitely isn’t one of them!
3 Likes

Especially when your program is still in the making and not yet finished, try-catch can obscure the true cause of an error. Very dangerous.

As far as I know it’s there to check when something breaks during you run the Sketch, e.g. a file you load is not there or an Internet resource. It’s a trick for the unforeseeable, not for the foreseeable.

NumberFormatException is a very notable case where its catch () {} block works as an if () {} replacement: :wink:

Er, no, definitely not what I meant. Never use try in place of if. The core JDK libraries are a bit lacking here in the lack of a tryParse function, but just because they’re lacking doesn’t make it a good idea. And with Processing, use the internal parsing functions that at least handle the catch / hide the exception for you. Still slow though! There are (Java) libraries that can do this more efficiently.

OT - When I said “never” use for logic, I had in mind a couple of ways libraries I know have used exceptions to support things that cannot otherwise be easily expressed. They will generally use their own exception classes too, to remove some of the things that make exception creation non-performant.