-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
I have read check documentation: https://checkstyle.org/checks/blocks/emptycatchblock.html
I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
From: https://google.github.io/styleguide/javaguide.html#s6.2-caught-exceptions
When it truly is appropriate to take no action whatsoever in a catch block, the reason this is justified is explained in a comment.
As the part of the fix, we need to:
-
Remove
exceptionVariableNameofEmptyCatchBlockCheck from the google-config
checkstyle/src/main/resources/google_checks.xml
Lines 434 to 436 in 185b0a8
<module name="EmptyCatchBlock"> <property name="exceptionVariableName" value="expected"/> </module> -
Find a way to check if the reason for swallowing the exception is actually appropriate.
Currently, any type of comment is valid ignoring the caught exception and can be misused as an excuse for not properly handling the exception.
/** some javadoc. */
public class Test2 {
/** some javadoc. */
void test1(Object response) {
try {
System.out.println("try");
} catch (NumberFormatException exception) {
// random keyword
} // ok above as any comment is excuse
}
void test2(Object response) {
try {
System.out.println("try");
} catch (NumberFormatException exception) { // it is OK
}
// ok above as any comment is excuse
}
void test3(Object response) {
try {
System.out.println("try");
} catch (NumberFormatException exception) {
/* random comment */
}
// ok above as any comment is excuse
}
}$ java -jar checkstyle-10.26.1-all.jar -c google_checks.xml Test2.java
Starting audit...
[WARN] /mnt/5D92528E6B945467/test/testing/Test2.java:16:47: Empty catch block. [EmptyCatchBlock]
Audit done.
google style does not give any recomendation on wording. So any text is ok, default of commentFormat is any.