Exceptions are for exceptional situations that you want to propagate back to the caller. Don't rely on exceptions during normal operation.
I would just write your code as:
public boolean canDivisionBeDone(int iA, int iB) {
return iB != 0;
}
But to answer your question: try-catch-finally
is implemented in Java using off-line exception tables, and as such, when no exception is thrown, is zero-overhead.
Here's how the bytecode of your two functions looks like:
public boolean canDivisionBeDoneTryCatch(int, int);
Code:
0: iload_1
1: iload_2
2: idiv
3: i2f
4: fstore_3
5: goto 11
8: astore_3
9: iconst_0
10: ireturn
11: iconst_1
12: ireturn
Exception table:
from to target type
0 5 8 Class java/lang/Exception
public boolean canDivisionBeDoneIf(int, int);
Code:
0: iload_2
1: ifne 6
4: iconst_0
5: ireturn
6: iload_1
7: iload_2
8: idiv
9: i2f
10: fstore_3
11: iconst_1
12: ireturn
As you can see the happy path is almost identical.
However, throwing an exception is expensive.
So yes, I would expect the exception version to be slightly slower, depending on the ratio of iB == 0
situations.
When in doubt, benchmark it.