AssertionError
JavaFATALNotableAssertion
Assertion failed — internal invariant violated
Quick Answer
Enable assertions with java -ea in development and test environments; do not use assert for production input validation (use IllegalArgumentException instead).
What this means
Thrown when an assert statement fails. Assertions are disabled by default in Java — they must be enabled with -ea (enableassertions). Use AssertionError to signal violated invariants in test code or defensive programming.
Why it happens
- 1An assert expression evaluates to false when assertions are enabled (-ea)
- 2Code flow reached a branch that should be unreachable
Fix
Use assert for internal invariants only
Use assert for internal invariants only
// Invariant check — never reachable in correct code
switch (status) {
case OPEN, CLOSED -> handle(status);
default -> throw new AssertionError("Unexpected status: " + status);
}Why this works
Throwing AssertionError explicitly in default/else branches documents the assumption that no other values can arrive, and surfaces bugs immediately if they do.
Code examples
Enable assertions in JVM argsjava
java -ea com.example.Main # Or in Maven Surefire: <argLine>-ea</argLine>
Do not use assert for validationjava
// Bad — skipped in production (assertions disabled by default) assert userId != null : "userId required"; // Good — always runs Objects.requireNonNull(userId, "userId required");
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