randomThought
randomThought

Reputation: 6393

How to handle error in this case? By Exceptions or boolean or something else

I am trying to create a Response class in Java which has a method

void setResponse(String response);

Different response subclasses will have different requirements for the response. The string that is passed to the function is received from the user.

What is the correct way of handling a wrong response?

  1. Should the function throw an exception like IllegalResponseException
  2. Should the function be declared like

    boolean setResponse(String response, String errorMsg)

and return false if the response is wrong and set the error message to the appropriate value

Edit: I want to design the UI so that I keep prompting the user for a response until a correct one is entered.

Upvotes: 2

Views: 1872

Answers (5)

Chatanga
Chatanga

Reputation: 21

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.

Upvotes: 0

DVK
DVK

Reputation: 129413

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).

Upvotes: 1

Oded
Oded

Reputation: 499052

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.

Upvotes: 2

polygenelubricants
polygenelubricants

Reputation: 383756

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.

Upvotes: 1

aib
aib

Reputation: 46961

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.

Upvotes: 0

Related Questions