SONARJAVA-6053 Fix S112 false positive for checked exception wrappers#5592
Conversation
Tests cover checked exceptions wrapped in RuntimeException or Error while preserving the caught cause directly, through getCause(), or through getTargetException(). They also keep coverage for generic, unchecked, message-only, and unrelated-cause wrappers that should still report. Relates to SONARJAVA-6053
Do not report raw RuntimeException or Error constructors when they are used inside a catch block to preserve a specific checked exception as the cause. The implementation follows the approved proposal by keeping throws-clause reporting unchanged, checking the enclosing catch type, and accepting direct caught exceptions plus getCause/getTargetException-derived causes. Relates to SONARJAVA-6053.
Handle checked-exception wrapper suppression when catch type semantics are degraded, while still requiring RuntimeException/Error wrappers to preserve the caught exception as the cause. Keep the non-compiling regression sample for an unresolved checked exception wrapper and align the ruling analysis with the current actual results.
Centralize raw exception type names in RawExceptionCheck so the S112 checked-exception wrapper logic no longer duplicates fully qualified class name literals reported by java:S1192. No suppressions were added.
Rule Profile
throw new Error("internal error");False Positive PatternThe false positives happen when code catches a specific checked exception and converts it to an unchecked wrapper because the enclosing API cannot or should not declare that checked exception. In that pattern, the generic wrapper type is intentional and the caught exception is preserved as the cause, so consumers still have the original failure context. try {
return invokeAsync(prompt).get();
}
catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}False Negative RiskThe suppression is intentionally narrow: it only applies to Fix SummaryThe implementation adds a catch-context check before reporting constructed raw exceptions. It recognizes preserved checked causes while leaving the rule's existing broad-exception behavior intact.
|
|
|
This PR is stale because it has been open 7 days with no activity. If there is no activity in the next 7 days it will be closed automatically |


Relates to SONARJAVA-6053
Suppress S112 when
RuntimeExceptionorErroris used in acatchblock to wrap a specific checked exception as the cause, while keeping existing reports for broad generic throws and wrappers that do not preserve the caught cause.throwsclause reporting unchanged.getCause()orgetTargetException().