I know we've had many fine threads on exceptions in Java. Hopefully this particular question hasn't been discussed to death.
I solved Problem 11 on Project Euler today. It requires you to do a bunch of multiplications on a 20 x 20 array, to find the largest product of four numbers taken on any line, column or diagonal.
Now, at first I tried to manually stay within the bounds if this int[][], but this was tedious and in the end failed to work correctly. So I rewrote the code as
which yielded the correct answer and much prettier code.
Is there anything wrong/bad form with this approach in my case? Is there anything wrong with catching unchecked exceptions in programs small enough that you "know" what you're doing?
I solved Problem 11 on Project Euler today. It requires you to do a bunch of multiplications on a 20 x 20 array, to find the largest product of four numbers taken on any line, column or diagonal.
Now, at first I tried to manually stay within the bounds if this int[][], but this was tedious and in the end failed to work correctly. So I rewrote the code as
Code:
try {
product *= grid[line][column];
product *= grid[line][column+1];
product *= grid[line][column+2];
product *= grid[line][column+3];
} catch (ArrayIndexOutOfBoundsException ex) {}
which yielded the correct answer and much prettier code.
Is there anything wrong/bad form with this approach in my case? Is there anything wrong with catching unchecked exceptions in programs small enough that you "know" what you're doing?