Hello all,
I've got a bit of a question about
Java idioms. One is about exceptions and the second about using assert vs. InvalidArgumentException.
First, exceptions: I'm having some trouble deciding between the following two idioms (assume these two methods are inside the same class):
or
In the first form, the higher level code (doFoo) is simpler, but it is not readily apparent where the InvalidBarException comes from without looking at verifyBar. The second form makes things clearer, I think, but may make doFoo harder to read, especially if exceptions are being logged and stack traces printed and so on. What's more, in the second form the if (isBarValid() != true) { ... } is used in several places, this could lead to a good amount of code duplication. The first method of course avoids this, there only has to be one if statement.
The second question is about the use of assert vs. IllegalArgumentException for indicating problems with parameters passed into methods. They seem almost identical to me except that assert isn't available pre 1.4. Is there an accepted idiom for this? Or should I just do what feels good (for me it's assert.)
So, does anyone have any opinions on these? I think I've got a good grasp on the language, but I'm still trying to get familiar with Java idioms.
Thanks and regards,
jb