[Java] Hidden catch block

java

(Werner Thumann) #1

Description
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.

Type
Code Smell

Snippet

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
			//...
		}
	}
	
}

Note
Also covered by Eclipse JDT.


(Alexandre Gigleux) #3

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

Regards