IllegalArgumentException
JavaERRORCommonValidation
Illegal or inappropriate method argument
Quick Answer
Validate arguments at method entry and throw IllegalArgumentException with a descriptive message.
What this means
Thrown to indicate that a method has been passed an illegal or inappropriate argument. Use it in your own APIs to enforce preconditions.
Why it happens
- 1Negative value passed where positive required
- 2Null passed to a method requiring non-null
Fix
Precondition check at method entry
Precondition check at method entry
void setAge(int age) {
if (age < 0 || age > 150)
throw new IllegalArgumentException("Invalid age: " + age);
this.age = age;
}Why this works
Failing fast at the entry point makes the bug site obvious in stack traces.
Code examples
Triggerjava
Thread.sleep(-1); // IllegalArgumentException
Custom validationjava
if (timeout <= 0)
throw new IllegalArgumentException("timeout > 0 required, got: " + timeout);Guava Preconditionsjava
Preconditions.checkArgument(n > 0, "n must be positive: %s", n);
Same error in other languages
Sources
Official documentation ↗
Java SE Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev