Java - catching unchecked Exceptions in small programs

Status
Not open for further replies.

Apteris

Ars Tribunus Angusticlavius
9,591
Subscriptor
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

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?
 
sryan2k1":3qrzahz6 said:
Relying on an exception to make your program operate properly is just awful. Especially for something as simple as known bounds on an array.
Relying on an exception is generally considered a bad practice because it goes against the way exceptions are generally used (for exceptional circumstances). However, it's only awful if the exception is not clearly specified to always be thrown in the scenario in which you're relying on it. For example, Java's Collections API states that ConcurrentModificationExceptions are thrown on a best-effort basis, and therefore relying on them being thrown if concurrent modifications occur is simply wrong. On the other hand, ArrayIndexOutOfBoundsExceptions are always thrown if you try to access an array at an invalid index, so relying on them is not quite so awful.
 

Apteris

Ars Tribunus Angusticlavius
9,591
Subscriptor
sryan2k1":2f5wi3l4 said:
Relying on an exception to make your program operate properly is just awful. Especially for something as simple as known bounds on an array.
Part of the problem, and this is something I realized only after posting the thread, is that I was computing the maximum product of any four or fewer adjacent numbers in the array, whereas the problem asks for the maximum product of precisely four numbers in the array. This led to me having more ifs than I needed, and a program that would fail if the last four elements in a row were 0, 99, 99, 99.

But assuming we'd been asked for the product of any four or fewer adjacent numbers, exceptions make the code shorter and simpler, and they behave in the same way as an if clause would, or if we avoid ifs, in the same way as manually making sure we don't go out of bounds.

andrewme":2f5wi3l4 said:
It's probably not exactly good for performance, depending on the ratio of the number of operations that throw to the number of operations that don't throw.

Quite right; I was a bit surprised at the performance difference. For an array of 20000 x 20000 bytes, solving with exceptions takes 37.893 seconds, and solving without exceptions, i.e.

Code:
//multiply to the right, down, up and to the right, down and to the right
for (int line = 3; line < grid.length-4; line++) {
	for (int coll = 0; coll < grid.length-4; coll++) { .. }
}

//now do down and to the right for lines 0-3
for (int line = 0; line<3; line++) {
	for (int column = 0; column < grid.length - 4; column++) { .. }

//finally do up and to the right for lines grid.length-4 to grid.length-1
	for (int line = grid.length-4; line<grid.length; line++)
		for (int column = 0; column < grid.length - 4; column++) { .. }

takes 19.494 seconds. Half the time.
 

norton_I

Ars Tribunus Angusticlavius
6,175
Subscriptor++
But assuming we'd been asked for the product of any four or fewer adjacent numbers, exceptions make the code shorter and simpler, and they behave in the same way as an if clause would, or if we avoid ifs, in the same way as manually making sure we don't go out of bounds.

You could pad the array with 1's on each side. Exceptions don't help you if there are other reasons to use less than 4 elements, like fractions or zeros. In that case, you might do better to parameterize your method by the number of consecutive elements to use, then try 1..4.
 
D

Deleted member 43669

Guest
Apteris":1okorhuh said:
exceptions make the code shorter and simpler,

I doubt so.

and they behave in the same way as an if clause would

No they don't. You said it was slower, for instance.

or if we avoid ifs, in the same way as manually making sure we don't go out of bounds.

Avoiding unnecessary branching in code is quite often good. I suggest you do that.
 
Status
Not open for further replies.