IllegalArgumentException
The standard unchecked exception for a method argument that is not acceptable, such as a blank identifier or a negative count.
In more detail
Throwing it in a record's compact constructor rejects invalid data at the boundary, so later code can trust the object. At the web boundary it usually becomes a 400 bad request response.
Example
throw new IllegalArgumentException("count cannot be negative");Related terms
- unchecked exception — An exception type, extending RuntimeException, that the compiler does not force callers to catch or declare.
- compact constructor — A record constructor written without a parameter list, which runs checks before the record's fields are assigned.
- invariant — A rule that must always hold for an object or system, such as a count never being negative.
Taught in
- Primitive And Reference Types — Modern Java: Virtual Threads & High-Throughput Services
- Records And Data Classes — Modern Java: Virtual Threads & High-Throughput Services
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Checked, Unchecked, Catch And Finally — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Exceptions, Arrays And Collections — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Paths And Files With NIO.2 — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep