API / Apache Grails Interview questions
What happens when a domain class fails validation in Grails?
Calling save() on a domain instance that violates one of its declared constraints doesn't throw an exception by default — instead, the save silently fails, the object is not persisted, and the instance is populated with errors describing exactly which constraints failed and why.
The developer is expected to check the return value or the instance's hasErrors() state after calling save(), and typically re-renders the form (in a traditional web flow) with the populated errors object, which GSP's error-rendering tags can display next to the specific fields that failed. Calling save(failOnError: true) changes this behavior, throwing a ValidationException immediately instead of failing silently, which is often preferred inside service-layer code where a caller expects either a successfully saved object or a clear exception, not a silently unsaved one that needs separate checking.
This default "fail quietly, populate errors" behavior mirrors the typical web-form flow (show the user what went wrong, let them fix it) while failOnError better matches programmatic, non-interactive contexts where an unexpected validation failure should surface loudly rather than be missed by a caller who forgot to check.
More Related questions...