4

in my team we use checkstyle to improve our coding standards, but now we came across a rule which could be improved.

The Empty Block rule gives us a warning about an empty catch block (without java code and without comment), but with the standard configuration it generates also a warning if the block contains a comment.

e.g.

The two should not result a warning:

try {
    // some code
} catch (NumberFormatException ignore) {
    // ignore
}

try {
    // some code
} catch (NumberFormatException e) {
    logger.debug("some debug");
}

This should result a warning:

try {
    // some code
} catch (NumberFormatException ignore) {

}

How can we improve checkstyle to give us only a warning, if no comment and no java code is in the catch block?

I looked for a solution, but I stackoverflow and google didn't have any.

Can someone help me?

duffy356
  • 3,678
  • 3
  • 32
  • 47

2 Answers2

5

The general question of how to tailor Checkstyle is ansered by this Question:

The Checkstyle documentation for tailoring the checking of blocks is here:

And the specific style configuration you need is:

  <module name="EmptyBlock">
    <property name="option" value="text"/>
    <property name="tokens" value="LITERAL_CATCH"/>
  </module>
Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

That problem was addressed at https://github.com/checkstyle/checkstyle/issues/571 , new Check was created - EmptyCatchBlock - http://checkstyle.sourceforge.net/config_blocks.html#EmptyCatchBlock

In configuration EmptyBlock , please remove CATCH token, as validation will done by different Check now

Roman Ivanov
  • 2,477
  • 3
  • 20
  • 31