Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convince me I'm wrong about using exceptions for user validation

Tags:

java

exception

It's hard to find a consensus, although many people say you should not use exceptions to handle bad user input. Still, I'm not convinced that it's the bad thing to do in my specific case. Could you try to explain why I'm wrong?

My code (please focus just on the exception handling aspect) follows. My rational for using exceptions here is that if I didn't, assuming I would want to keep the validation logic close to the keyword parsing (because parsing and validation are tightly coupled), I would have to change three methods (submitOnAdd, submitOnUpdate, getKeywords) to make them handle this exceptional situation. Do you think I definitely wrong in this case to use exceptions, or is it a matter of personal style?

public SubmitResponse internalSubmit(Map<String, String[]> submitParameters) {
  try {
      if (!submitParameters.containsKey("foo")) {
        return submitOnAdd(submitParameters);
      } else {
        return submitOnModify(submitParameters);
      }
  } catch (SubmitErrorException e) {
      return SubmitResponse.fieldError(Arrays.asList(e.getSubmitError()));
  }
}

SubmitResponse submitOnAdd(Map<String, String[]> submitParamters) {
  // do some stuff
  // ...
  if (addKeywordList(createKeywordList(submitParameters.get("concatenated_keywords"))
    return SubmitResponse.OK();
  return SubmitResponse.bad("Failed to add");
}

SubmitResponse submitOnUpdate(Map<String, String[]> submitParamters) {
  // do some other stuff
  // ...
  if (updateKeywordList(createKeywordList(submitParameters.get("concatenated_keywords"))
    return SubmitResponse.OK();
  return SubmitResponse.bad("Failed to update");
}

List<Keyword> getKeywords(String concatenatedKeywords) {
  List<String> rawKeywords = splitKeywords(concatenatedKeywords);
  return Collections.transform(new Function<String, Keyword>() {
      @Override
      public KeywordListProto.Keyword apply(String expression) {
        return buildKeyword(expression);
      }
    });
}

private Keyword buildKeyword(String rawKeyword) {
  // parse the raw keyword
  if (/*parsing failed */)
    throw new SubmitResponseException("Failed to parse keyword " + rawKeyword);

  return parsedKeyword;
}
like image 910
ripper234 Avatar asked Aug 25 '26 03:08

ripper234


1 Answers

I can't say I would never advise to use Exceptions somewhere in input validation. But in this case, I would say it adds to much confusion. I would either:

  • Add a separate method to handle the validation. (Might have to call this method in several places which is a negative, but it might make the code easier to understand).
  • In the more ideal case, I would validate closer to the user input and not allow a submit of invalid data. (A possible negative is separation of validation and parsing logic, but if you could somehow use the same class to do both this could be avoided).
like image 131
jzd Avatar answered Aug 26 '26 18:08

jzd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!