Aquila Deus said:
I see. But my reason to use exceptions is that the same ActionForm may
be used by web service or some agent program.
Do you mean another program might pass data to your application? A web service
or other program should not use an ActionForm from your application. ActionForms
are internal to the Struts framework, and I think it would generally be
considered bad design to pass them around to other applications. And if you do
mean another application is simply sending requests to your application, are you
going to serialize the exception and send it back? Encode it in the response? Or
are you going to send back some kind of error code? I think the latter is more
likely, and in that case you still don't have any use for an exception.
Whoever/whatever your "user" is, you're unlikely to want he/she/it to see an
exception.
Thus it may be better to
throw exceptions at ActionForm's code. If the user is a human, struts
should be catch the exception and give him a human-readable error
message instead (not directly - the msg is saved to some variable for
jsp to display).
Again, humans entering invalid data is not exceptional. You're also overlooking
the likely circumstance of multiple validation errors. If your ActionForm throws
an exception at the first validation error, you're looking at multiple form
submissions to get all of the errors worked out since you can only alert the
user to one at a time. That would be annoying at best.
Besides, isn't it more natural for a ActionForm
developer to throw exceptions (just what they do in ordinary beans)
Ordinary beans throw exceptions? What do you mean?
rather than return an error message? Since ActionForm is not accessed
by end-user directly, I think the job to present its exception/error is
jsp and struts' job.
It is Struts' job to present errors, but I still maintain that the errors
shouldn't be generated by exceptions because validation errors are not
exceptional.