KEMBAR78
Google style: No proper way to check if the reason for swallowing the exception is actually appropriate · Issue #17563 · checkstyle/checkstyle · GitHub
Skip to content

Google style: No proper way to check if the reason for swallowing the exception is actually appropriate #17563

@mohitsatr

Description

@mohitsatr

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:

  1. Remove exceptionVariableName of EmptyCatchBlock Check from the google-config

    <module name="EmptyCatchBlock">
    <property name="exceptionVariableName" value="expected"/>
    </module>

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions