IllegalArgumentException means the caller passed a value the method cannot accept, while IllegalStateException means the arguments were fine but the object itself is not currently in a state where the call is allowed.
class Counter {
private int value;
private boolean started;
void start() {
started = true;
}
void increment(int amount) {
if (amount < 0) {
throw new IllegalArgumentException("amount cannot be negative: " + amount);
}
if (!started) {
throw new IllegalStateException("counter has not been started");
}
value += amount;
}
}
Counter counter = new Counter();
try {
counter.increment(5);
} catch (IllegalStateException e) {
System.out.println("State: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
counter.start();
try {
counter.increment(-1);
} catch (IllegalArgumentException e) {
System.out.println("Argument: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
State: IllegalStateException: counter has not been started
Argument: IllegalArgumentException: amount cannot be negative: -1
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27