Do Not Throw java.lang.Error

ID

java.no_throw_error

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:397, reliability

Description

Reports throw new Error(…​) statements.

Rationale

java.lang.Error signals an unrecoverable JVM-level problem (e.g. OutOfMemoryError, StackOverflowError). Throwing Error directly from application code misleads callers and monitoring tools into treating a recoverable application fault as a JVM failure. Callers are not expected to catch Error, so the exception may propagate unchecked past recovery logic.

// Bad — callers have no reason to catch Error
if (config == null) {
    throw new Error("missing config");
}

Remediation

Throw an appropriate RuntimeException subclass instead:

// Good — clear and catchable
if (config == null) {
    throw new IllegalStateException("missing config");
}