[Java] Hidden catch block

A catch block of a checked exception E may be hidden because the corresponding try block only throws exceptions derived from E and these are handled in catch blocks prior to the catch block of the base exception E. This is essentially dead code and could indicate a bug.

Code Smell


public class HiddenCatchBlock {

	public static class CustomException extends Exception {
	public static class CustomDerivedException extends CustomException {
	public static void main(String[] args) {
		try {
			throw new CustomDerivedException();
		} catch(CustomDerivedException e) {
		} catch(CustomException e) { // Noncompliant

Also covered by Eclipse JDT.

Hello Werner,

Thanks to your great specification work, I created https://jira.sonarsource.com/browse/RSPEC-4970 and its related implementation ticket: https://jira.sonarsource.com/browse/SONARJAVA-2944