Validation - Throw Exception or Return False?

Jakob Jenkov
Last update: 2014-06-23

When validating input data you do not always have to throw an exception if the input is invalid. How to handle invalid data or state sensibly often depends on the condition:

Can the code detecting the error, interact sensibly with the
user, to correct the error?

If the error is caused by input from a user, and that user is able to correct the input and retry, then interacting is often preferrable to throwing an exception. In addition you might want to detect all possible errors in the input before showing them to the user. That way the user can correct all the errors before next retry, instead of repeated iterations of correcting, retrying, correcting, retrying etc. For instance, in a servlet you could do like this:

public void service(HttpServletRequest request,
                  HttpServletResponse response){
  boolean isStreetvalid  = validateStreet (request);
  boolean isZipCodeValid = validateZipCode(request);
  boolean isPhoneValid   = validatePhone  (request);

  if(isStreeValid && isZipCodeValid && isPhoneValid){
      //perform action
  } else {
      //redirect back to HTML form and show the three errors.
  }

}

Notice how none of the validation methods throw exceptions. Rather they return true or false. There is no need to throw an exception even if the input data is invalid.

In contrast, inside a DAO class you will most likely not be able to interact with the user to correct the error. A DAO object may not even know if it is called from inside a web service, a servlet, or somewhere else. It has no way of knowing if the input came from another system, a file, or a user. It has no way of knowing how, or even if, the error could be sorted out through interact with the user.

In this case the standard way for the DAO object to signal the error is to throw an exception. Other mechanisms like return codes etc. could be used, but it doesn't change the fact that the DAO cannot handle the error itself. The input data should have been validated before the call to the DAO. Here is how it could look:

public void insertAddress(Address address){

  validate(address);

  //insert address
}


public void validate(Address address) {

   if(isInvalidCity(address.getCity()){
       throw new InvalidArgumentException(
        "City " + address.getCity() + " is not valid");
   }

   // more if-statements like the one above,
   // validating each field in the address object.
}

In this example the validate() method throws an exception as soon as an validation error is found. The insertAddress method cannot interact with the user to correct the error. Thus it is appropriate to throw an exception to signal the error. Alternatively all errors could be detected before throwing the exception.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next