How to deal with the error in this case? Exceptions or booleans or whatever

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

void setResponse (line response);

Different subclasses of responses will have different response requirements. The string passed to the function was received from the user.

What is the correct way to handle a wrong answer?

  • If the function throws an exception like IllegalResponseException
  • If the function is declared as

    boolean setResponse (string response, String errorMsg)

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

Edit: I want to create a UI so that I keep asking the user for an answer until the correct one is entered.

+2


a source to share


5 answers


It depends on what you want the program to do when the wrong answer is set.

If it is 100% critical and the program should not continue in this case, throw an exception.



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

An excellent discussion of exceptions for error handling - here - does not directly address exceptions and return codes, but a very clear list of the downsides of criticizing checked exceptions also applies to this discussion (again, this assumes that the error you are handling is not supercritical, which should cause the program to interrupt, in which case the unchecked exception is correct).

+1


a source


Reserve exceptions for exceptional circumstances.



If you expect errors, your code should treat them as a matter of course, rather than throw and throw exceptions all over the place.

+2


a source


The second option is out of the question, since String

Java is immutable. If you think you can do errorMsg = "Wrong response!"

and have this useful value, then you need to step back right now and do some reading about what it means to String

be immutable, and what it means that Java passes all references to the value.

The first option exists IllegalArgumentException

, which is often used for this purpose, so you can use that instead of your own custom exception class. He is extends RuntimeException

, which means his exclusion.

+1


a source


I don't see how the String

value could be wrong, especially if passed to the setter function. So you should probably use an exception and maybe throw it when a [wrong] string is used, not set.

It is also probably not responsible for setting the string to the error message string. I would consider returning more compact error information (i.e. Boolean or enumeration or even a class if the error has to be a lot of detail) and extend it in another class.

0


a source


Only use an exception for the case of an error, which should not occur because the caller has (or may have) sufficient knowledge to avoid it. In your case, the validity of the answer seems to be hidden in your subclass implementation, since the caller may not know the "correct" answer. Not necessarily a problem (for example, the response might be a password), but you should be returning an error code rather than throwing an exception.

0


a source







All Articles