PDA

View Full Version : Checked vs unchecked exception


scesbron
Sep 9th, 2004, 04:30 AM
I have a rule that says that my password must be more that 4 characters. So in my User domain object I have to check this in the setPassword method.

If the rule is broken I throw an exception. I can't decide wether I throw a checked or unchecked exception in this case. Can somebody advise me ?

It's an exception the caller can handle so Rod's book advise to throw a checked exception. But if I do that, lot of my setters will throw exception and this may lead in a lot of unnecessary catch blocks.

Thanx

Seb

Rod Johnson
Sep 9th, 2004, 05:25 AM
If you want to keep the validation in the domain object I would make the exception checked. An alternative might be to remove the syntactic validation from the domain object into a validator.

I don't understand why using a checked exception would impact so many setters--in how many places do you set the password property?

cesbron
Sep 10th, 2004, 02:30 AM
I don't understand why using a checked exception would impact so many setters--in how many places do you set the password property?

No, I mean that I have thus kind of validation (non null, max size, format) in all my setters so all the classes that use my domain objects have to deal with this kind of exceptions. I am thinking of creating a DomainCheckException and all setters will throw some subclasses of this exception