+1  A: 

This depends somewhat on what you want the program to do when incorrect response is set.

If it's a 100% critical thing and the program should never proceed in such a case, throw an exception.

If it needs to be handled correctly by the caller and continue execution of the program, you can do either one, but I personally prefer #2. Why?

A great discussion of exceptions for error handling is here - it does not directly address exceptions vs. return codes but a very clear list of downsides of criticisms of checked exceptions applies to this discussion as well (again, this is assuming the error you're handling is not a super-critical one which should cause program termination, in which case an unchecked exception is proper).

DVK
Using number 2, how can I check what reason caused the error. Should I set the return value as a String and code it so that a blank String means that the set function returned no errors?
TP
@TP - you can do that. Depends on whether you need the reason for display to the user (a string would be perfect) or for programmatic reasons (e.g. a limited-value return type would be needed to determine what the program should do in case of an error based on what the reason is)
DVK
I would want to display the string to the user.
TP
A: 

I don't see how a String can have a wrong value, especially one passed to a setter function. As such, you should probably use an exception and maybe throw it when the [wrong] string is used, not set.

It is also probably not the setter's responsibility to generate an error message string. I would consider returning more compact error information (i.e. a boolean or an enumeration, or even a class if the error should contain a lot of details) and expand on it in another class.

aib
A response, in my case, needs to be comma seperated for some subclasses, or needs to be certain words for some subclasses so there can be errors in the string.
TP
+1  A: 

The second option is out of the question since String in Java is immutable. If you think that you can do errorMsg = "Wrong response!" and have that be useful, then you need to step back right now and do some reading on what it means for String to be immutable, and what it means that Java passes all references by value.

For the first option, there is an IllegalArgumentException that is often used for this purpose, so you can use that instead of your own custom exception class. It extends RuntimeException, meaning it's an unchecked exception.

polygenelubricants
I forgot about the whole immutable Strings thing in Java. I was thinking in terms of C++ and passing by reference.
TP
+2  A: 

Reserve exceptions for exceptional circumstances.

If you expect errors, your code should handle them as a matter of course, not throw and catch exceptions all over the place.

Oded
A: 

Only use exception for error case which should not happen because the caller have (or could have) the sufficient knowledge to avoid it. In your case, the validity of a response seems to be hidden in your subclass implementation, with no way for the caller to know the "right" answer. Is not necessarily a problem (the response could be a password for instance), but you should return an error code instead of throwing an exception.

Chatanga