Exception Created But Not Thrown

ID

java.exception_not_thrown

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports new SomeException(…​) appearing as a standalone expression statement — the exception is created but never thrown, assigned to a variable, or passed as a method argument. This is almost certainly a bug where throw was forgotten.

Rationale

Creating an exception without throwing it has no observable effect. The object is immediately eligible for garbage collection. The developer almost certainly intended throw new …​.

// Bad - exception is created and discarded
new IllegalArgumentException("invalid input");

Remediation

Add the throw keyword.

// Good
throw new IllegalArgumentException("invalid input");