Illegal arguments passed to the Bean Validation APIs generally lead to a IllegalArgumentException (see JavaDoc for specific details). Other exceptions raised by Bean Validation are or inherits from the runtime exception javax.validation.ValidationException. Exception cases are described in their respective sections but include (non exhaustive list):
invalid constraint definitions (missing mandatory elements, illegal composition cycle, illegal parameter overriding, etc)
invalid constraint declarations (ConstraintValidator implementation matching failure, etc)
invalid group definition (circularity)
invalid Default group redefinition for classes (missing class group etc)
error when retrieving, initializing, executing ConstraintValidators
error when parsing the XML configuration or mappings
multiple XML configuration files found
missing expected provider or no default provider found
missing no-arg constructors on extension implementations described in the XML configuration file
same entity described more than once across the XML mapping files
same property or field described more than once for a given entity in the XML mapping files
class, field or getter declared in XML mapping files but not found
illegal XML constraint definition
illegal XML constraint declaration
exception raised either at initialization time or execution time by any of the extension interfaces
Each of these error cases lead to a ValidationException or a subclass of ValidationException (see following subsections).
Every (runtime) exception raised either at initialization time or execution time by any of the extension interfaces (ConstraintValidator, ConstraintValidatorFactory, MessageInterpolator,TraversableResolver, ValidationProviderResolver) is wrapped in a ValidationException.
If a constraint definition or constraint declaration is invalid for a given class, the metadata API should raise the according exception.
Some frameworks or applications need to convey the results of a validation. It is an exception case if the validation returns constraint violation and a Java exception is raised.
Bean Validation provides a reference exception for such cases. Frameworks and applications are encouraged to use ConstraintViolationException as opposed to a custom exception to increase consistency of the Java platform. The exception can be raised directly or wrapped into the framework or application specific mother exception.
/** * Report the result of constraint violations * * @author Emmanuel Bernard */ public class ConstraintViolationException extends ValidationException { /** * Creates a constraint violation report * * @param message error message * @param constraintViolations Set of ConstraintViolation */ public ConstraintViolationException(String message, Set<ConstraintViolation> constraintViolations) { [...] } /** * Creates a constraint violation report * * @param constraintViolations Set of ConstraintViolation */ public ConstraintViolationException(Set<ConstraintViolation> constraintViolations) { [...] } /** * Set of constraint violations reported during a validation * * @return Set of CosntraintViolation */ public Set<ConstraintViolation> getConstraintViolations() { [...] } }
The ConstraintViolationException carries a set of ConstraintViolation.
Bean Validation never raises this exception itself. Other frameworks like Java Persistence 2 do.
If a constraint definition does not respect the Bean Validation rules or is inconsistent, a ConstraintDefinitionException is raised.
This exception can be raised during validation or when the metadata model for the class hosting this constraint is requested.
These exception cases can be determined at compile time by a tool such as an annotation processor.
When a constraint declaration is illegal, ConstraintDeclarationException is raised.
When the return type of a property cannot be processed for a given constraint, an UnexpectedTypeException is raised. This problem typically arise when either no ConstraintValidator or too many ConstraintValidators match the return type (see Section 3.5.3, “ConstraintValidator resolution algorithm”).
This exception can be raised during validation or when the metadata model for the class hosting this constraint is requested.
These exception cases can be determined at compile time by a tool such as an annotation processor.