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?

1

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

While this was true when answered, it is now EmptyCatchBlock as mentioned below.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.