Re: Exception handling
On Fri, 27 Nov 2009, Pitch wrote:
These are my general rules:
- layer X catches everything but RuntimeExceptions and throws
"X_Exception"
- layer Y catches everything but RuntimeExceptions and throws
"Y_Exception"
- GUI/presentation/interface layer catches everything and logs it or
displays a dialog or whatever.
This is pretty much the Right Way To Do It.
But note that in this case, you have to be careful about using
IllegalArgumentException, as it's a RuntimeException. If code in a deep
layer is doing input validation (say, taking a user-entered string and
looking something up in a database with it), then failure of validation
needs to be signalled with a checked layer-specific exception, otherwise
it will go through the upper layers and annoy the user.
On the other hand, if validation fails for a reason which could have been
caught by an upper layer (a parameter is null, an argument is too long or
the wrong colour, etc), then that's a programming error on the part of the
upper layer, and it's okay to throw an IllegalArgumentException. Although
it might still be wise to throw a checked exception.
tom
--
.... including a depiction of evolution that would make a furious Charles
Darwin eat his own beard. -- Ben Croshaw, on Spore