Re: Design Questions about static factory classes
On 05/22/2010 03:05 PM,
Arne Vajh??j wrote:
That problem is not so important. You would not want to display
Java exception text to end-users anyway.
Really? I find that a surprising thing to say.
Exceptions are not for users, they're for code.
Maybe we're not talking about the same thing.
I'm thinking of a situation like completing a form in a GUI. The customer
has to enter his date of birth. Let's say that I can't use a JSpinner for
some reason; it can't do everything I need it to do. The customer is
given a simple JTextField for entering a date. Clearly, the customer
would have many opportunities to enter bad data. He could type in 1985-
15-31 when he meant to type 1985-05-01; the first value is obviously a
bad date since there are only 12 months in the year, not 15. My practice
is to write edits that check for that kind of mistake and generate an
exception, typically IllegalArgumentException, with a clear error message
that reminds the user that there is no such month as '15'. Naturally, the
customer might not be an English speaker so I put all such messages in
ResourceBundles so that other bundles can easily be added for any
languages that I support.
You generate an error message for the customer, but it's not in the exception,
it's in what *handles* the exception.
In fact, with input validation, you wouldn't want to create, much less throw
an exception at all. Bad data is not part of the exceptional flow, it's part
of the normal flow and should be handled with conditionals.
Furthermore, runtime exceptions like IllegalArgumentException are for
programmer errors, not situational problems like failure to locate a file
(FileNotFoundException) or socket woes (SocketException), which are covered by
checked exceptions. You certainly don't want to talk to the user about
How would you handle such a situation?
Not use exceptions at all.