Subclass of Error or Throwable

ID

java.subclass_error

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:397, reliability

Description

Reports classes that directly extend Error, java.lang.Error, Throwable, or java.lang.Throwable.

Rationale

The Error hierarchy is reserved for unrecoverable JVM-level conditions such as OutOfMemoryError and StackOverflowError. Callers are not expected to catch or recover from these. Extending Error for application-level exceptional conditions confuses the exception taxonomy and may bypass catch blocks that only handle Exception.

Extending Throwable directly bypasses the checked/unchecked distinction entirely and creates throwables that are neither Exception nor Error, making proper handling virtually impossible.

// Bad -- application class extends Error
public class AppError extends Error {
    public AppError(String msg) { super(msg); }
}

Remediation

Extend Exception (for checked exceptions) or RuntimeException (for unchecked exceptions) instead:

// Good -- extends Exception
public class AppException extends Exception {
    public AppException(String msg) { super(msg); }
}

// Good -- extends RuntimeException
public class AppRuntimeException extends RuntimeException {
    public AppRuntimeException(String msg) { super(msg); }
}