IllegalArgumentException is thrown when a method receives a value that has the correct type but an invalid meaning. The object exists and the method is available, but the argument breaks the rule expected by that method.
This exception is common in constructors, setters, validation methods, enum conversion, date parsing, range checks, and service-layer code. A good Java page should teach both the cause and the prevention pattern: validate early, give a clear message, and keep the object in a valid state.
When debugging it, do not only ask which line failed. Ask which argument was passed, what rule the method expected, and whether the caller or the method should be responsible for checking that rule.
IllegalArgumentException is different from NullPointerException and ClassCastException. It usually means the caller passed something like a negative age, an empty username, an unsupported enum value, or a number outside the allowed range.
The argument type can still be correct. For example, setAge(int age) receives an int, but -5 is not a valid age for most applications. Java cannot know that business rule automatically, so your code must check it.
Read the stack trace from top to bottom and find the first line that belongs to your code. Then inspect the argument passed into that method. The fix may be in the caller, not inside the method that throws the exception.
For reusable classes, keep validation inside constructors and setters. For user input, validate at the boundary, convert the input carefully, and show a user-friendly message instead of exposing the raw exception.
In interviews, explain that IllegalArgumentException is unchecked and usually signals wrong API usage by the caller. It is useful when a method contract is broken before the method can do meaningful work.
Also mention that throwing it should not replace all validation. In web apps, input validation should normally produce a controlled response, while IllegalArgumentException is more useful inside domain or utility code.
Choose the exception that identifies the broken contract. IllegalArgumentException means the caller supplied an unacceptable value. IllegalStateException means the receiver cannot perform the operation in its current state, even if the argument is valid.
Use NullPointerException for a required reference that is null and an index-specific exception for an invalid position. A domain API may instead expose a named exception when callers need to distinguish a business rejection from programmer misuse.
class UserProfile {
private final int age;
UserProfile(int age) {
if (age < 0 || age > 130) {
throw new IllegalArgumentException("age must be between 0 and 130: " + age);
}
this.age = age;
}
}
public class PageRequestDemo {
static void loadPage(int pageSize) {
if (pageSize <= 0) {
throw new IllegalArgumentException("pageSize must be positive");
}
System.out.println("Loading " + pageSize + " records");
}
public static void main(String[] args) {
int pageSize = 25;
loadPage(pageSize);
}
}
Throw it when a method receives an argument that violates its contract, such as a negative value where a positive value is required or an option outside the supported set.
IllegalArgumentException is about invalid input to a method. IllegalStateException is about the object being in an invalid state for the operation (e.g., calling close() on an already-closed resource).
Java API convention generally favors NullPointerException for a required reference that is null; Objects.requireNonNull applies that convention. Use IllegalArgumentException for non-null values that violate another method rule, and document the public contract.
Explore 500+ free tutorials across 20+ languages and frameworks.