Since Checkstyle 6.4
Checks for empty catch blocks. By default, check allows empty catch block with any comment inside.
There are two options to make validation more precise: exceptionVariableName and commentFormat. If both options are specified - they are applied by any of them is matching.
name | description | type | default value | since |
---|---|---|---|---|
exceptionVariableName | Specify the RegExp for the name of the variable associated with exception. If check meets variable name matching specified value - empty block is suppressed. | Pattern | "^$" |
6.4 |
commentFormat | Specify the RegExp for the first comment inside empty catch block. If check meets comment inside empty catch block matching specified format - empty block is suppressed. If it is multi-line comment - only its first line is analyzed. | Pattern | ".*" |
6.4 |
To configure the check:
<module name="EmptyCatchBlock"/>
Example:
try { throw new RuntimeException(); } catch (RuntimeException expected) { } // violation try { throw new RuntimeException(); } catch (RuntimeException ignore) { // no handling } // ok, catch block has comment try { throw new RuntimeException(); } catch (RuntimeException o) { } // violation try { throw new RuntimeException(); } catch (RuntimeException ex) { // This is expected } // ok
To configure the check to suppress empty catch block if exception's variable name is
expected
or ignore
or there's any comment inside:
<module name="EmptyCatchBlock"> <property name="exceptionVariableName" value="expected|ignore"/> </module>
Such empty blocks would be both suppressed:
try { throw new RuntimeException(); } catch (RuntimeException expected) { } // ok try { throw new RuntimeException(); } catch (RuntimeException ignore) { // no handling } // ok try { throw new RuntimeException(); } catch (RuntimeException o) { } // violation try { throw new RuntimeException(); } catch (RuntimeException ex) { // This is expected } // ok
To configure the check to suppress empty catch block if single-line comment inside is "//This is expected":
<module name="EmptyCatchBlock"> <property name="commentFormat" value="This is expected"/> </module>
Such empty block would be suppressed:
try { throw new RuntimeException(); } catch (RuntimeException expected) { } // violation try { throw new RuntimeException(); } catch (RuntimeException ignore) { // no handling } // violation try { throw new RuntimeException(); } catch (RuntimeException o) { } // violation try { throw new RuntimeException(); } catch (RuntimeException ex) { // This is expected } // ok
To configure the check to suppress empty catch block if single-line comment inside is "//This is expected" or exception's variable name is "myException" (any option is matching):
<module name="EmptyCatchBlock"> <property name="commentFormat" value="This is expected"/> <property name="exceptionVariableName" value="myException"/> </module>
Such empty blocks would be suppressed:
try { throw new RuntimeException(); } catch (RuntimeException e) { //This is expected } ... try { throw new RuntimeException(); } catch (RuntimeException e) { // This is expected } ... try { throw new RuntimeException(); } catch (RuntimeException e) { // This is expected // some another comment } ... try { throw new RuntimeException(); } catch (RuntimeException e) { /* This is expected */ } ... try { throw new RuntimeException(); } catch (RuntimeException e) { /* * * This is expected * some another comment */ } ... try { throw new RuntimeException(); } catch (RuntimeException myException) { }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.blocks