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");
Scope
The allocation must be the statement’s own expression. An exception passed on as an argument is used, not discarded, so none of these is reported:
when(service.read()).thenThrow(new IllegalStateException("boom")); // Mockito stubbing
doThrow(new IOException("boom")).when(service).read();
logger.error("shutdown failed", new IllegalStateException("stack")); // captures a stack trace
value.orElseThrow(() -> new IllegalStateException("absent"));
new IllegalStateException("boom").printStackTrace(); // the object is used