Re: Design Questions about static factory classes
On 24-05-2010 08:12, Tom Anderson wrote:
On Sun, 23 May 2010, Lew wrote:
Arne Vajh?j wrote:
Bad user input is not really exceptional enough to justify an
exception.
Tom Anderson wrote:
I disagree. We've had arguments about the proper use of exceptions on
this newsgroup before, so i [sic] recognise that this is a matter where
opinions vary, but exceptions seem like a perfectly acceptable option
for dealing with bad user input to me. They might not be the right
solution in every situation, but they are an option that can be
considered.
And usually rejected.
Read /Effective Java/ (2nd ed.), "Item 57: Use exceptions only for
exceptional conditions", and the rest of section 9.
No.
It is always good to read opinions from qualified people even
if you disagree with them.
Part of the problem with exceptions is that they are expensive
relative to conditionals.
True. I wouldn't worry about that in input validation code of this kind
unless i had a profiler screaming at me that it was a problem.
Hmm. Do profilers track the amount of time the JVM spends handling
exceptions?
If it is interactive user input then the overhead of exceptions
will be unmeasurable due the ratio between CPU speed and human
typing speed.
I don't think performance is the best argument against exceptions
for bad user input.
The other part in this case is that you expect bad inputs - they
aren't exceptional conditions at all.
Your style is your style, tom, and you are absolutely correct to
suggest that one should consider all options. But the design purpose
of exceptions is to deal with out-of-line conditions, and input
validation is squarely in line.
I don't agree that bad input is not an exceptional condition; i wonder
if you are confusing 'exceptional' and 'unlikely' or 'unexpected'. I
agree that validating input must be done inline, but not that dealing
with invalid input must be. I'd say exactly the opposite, in fact.
You are free to have your opinion - just note that a lot
of people disagree today.
Arne