Configure checkstyle to ignore an empty catch block

in my team we use checkstyle to improve our coding standards, but now we are faced with a rule that can be improved.

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

eg.

Both should not cause warnings:

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

This will result in a warning:

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

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

I was looking for a solution, but stackoverflow and google didn't have one.

Can someone help me?

+4
source share
2 answers

The general question about how to configure Checkstyle is determined by this question:

Checkstyle documentation for setting up block checking is here:

And the specific style configuration you need:

  <module name="EmptyBlock"> <property name="option" value="text"/> <property name="tokens" value="LITERAL_CATCH"/> </module> 
+5
source

This problem was resolved at https://github.com/checkstyle/checkstyle/issues/571 , a new check was created - EmptyCatchBlock - http://checkstyle.sourceforge.net/config_blocks.html#EmptyCatchBlock

In the EmptyBlock configuration, please remove the CATCH token, as the verification will be done differently. Check now

+2
source

All Articles