Pattern: Use of empty catch
block
Issue: -
Except as noted below, it is very rarely correct to do nothing in response to a caught exception. Typical responses are to log it, or if it is considered "impossible", rethrow it as an AssertionError
.
When it truly is appropriate to take no action whatsoever in a catch block, this rule enforces to have a comment to explain the reason this is justified.
Exception: In tests, a caught exception may be ignored without comment if its name is or begins with expected
or ignore
(configurable). The following is a very common idiom for ensuring that the code under test does throw an exception of the expected type, so a comment is unnecessary here.
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="expected|ignore"/>
</module>
Example of incorrect code:
try {
int i = Integer.parseInt(response);
return handleNumericResponse(i);
} catch (NumberFormatException exception) {
}
return handleTextResponse(response);
Example of correct code:
try {
int i = Integer.parseInt(response);
return handleNumericResponse(i);
} catch (NumberFormatException exception) {
// it's not numeric; that's fine, just continue
}
return handleTextResponse(response);
To configure the check to suppress empty catch
block if single-line comment inside is //This is expected
or exception's variable name is myException
(any option is matching):
<module name="EmptyCatchBlock">
<property name="commentFormat" value="This is expected"/>
<property name="exceptionVariableName" value="myException"/>
</module>