EmptyCatchBlock
Since Checkstyle 6.4
Description
Notes
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.
Properties
name | description | type | default value | since |
---|---|---|---|---|
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 |
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 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="EmptyCatchBlock"/>
</module>
</module>
Such empty blocks would be both suppressed:
public class Example1 {
private void exampleMethod1() {
try {
throw new RuntimeException();
} catch (RuntimeException expected) {
} // violation above 'Empty catch block'
}
private void exampleMethod2() {
try {
throw new RuntimeException();
} catch (RuntimeException ignore) {
// no handling
} // ok, catch block has comment
}
private void exampleMethod3 () {
try {
throw new RuntimeException();
} catch (RuntimeException o) {
} // violation above 'Empty catch block'
}
private void exampleMethod4 () {
try {
throw new RuntimeException();
} catch (RuntimeException ex) {
// This is expected
}
}
}
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="Checker">
<module name="TreeWalker">
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="expected|ignore"/>
</module>
</module>
</module>
Such empty blocks would be both suppressed:
public class Example2 {
private void exampleMethod1() {
try {
throw new RuntimeException();
} catch (RuntimeException expected) {
}
}
private void exampleMethod2() {
try {
throw new RuntimeException();
} catch (RuntimeException ignore) {
// no handling
}
}
private void exampleMethod3 () {
try {
throw new RuntimeException();
} catch (RuntimeException o) {
} // violation above 'Empty catch block'
}
private void exampleMethod4 () {
try {
throw new RuntimeException();
} catch (RuntimeException ex) {
// This is expected
}
}
}
To configure the check to suppress empty catch block if single-line comment inside is "This is expected":
<module name="Checker">
<module name="TreeWalker">
<module name="EmptyCatchBlock">
<property name="commentFormat" value="This is expected"/>
</module>
</module>
</module>
Such empty blocks would be both suppressed:
public class Example3 {
private void exampleMethod1() {
try {
throw new RuntimeException();
} catch (RuntimeException expected) {
} // violation above 'Empty catch block'
}
private void exampleMethod2() {
try {
throw new RuntimeException();
} catch (RuntimeException ignore) {
// no handling
} // violation 2 lines above 'Empty catch block'
}
private void exampleMethod3 () {
try {
throw new RuntimeException();
} catch (RuntimeException o) {
} // violation above 'Empty catch block'
}
private void exampleMethod4 () {
try {
throw new RuntimeException();
} catch (RuntimeException ex) {
// This is expected
}
}
}
To configure the check to suppress empty catch block if single-line comment inside is "This is expected":
<module name="Checker">
<module name="TreeWalker">
<module name="EmptyCatchBlock">
<property name="commentFormat" value="This is expected"/>
</module>
</module>
</module>
Such empty blocks would be both suppressed:
public class Example4 {
private void exampleMethod1() {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
//This is expected
}
}
private void exampleMethod2() {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
// This is expected
}
}
private void exampleMethod3 () {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
// This is expected
// some another comment
}
}
private void exampleMethod4 () {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
/* This is expected */
}
}
private void exampleMethod5() {
try {
throw new RuntimeException();
// violation below 'Empty catch block'
} catch (RuntimeException e) {
/*
*
* This is expected
* some another comment */
}
}
}
To configure the check to suppress empty catch block if exception's variable name is "myException":
<module name="Checker">
<module name="TreeWalker">
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="myException"/>
</module>
</module>
</module>
Such empty blocks would be both suppressed:
public class Example5 {
private void exampleMethod1() {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
} // violation above 'Empty catch block'
}
private void exampleMethod2() {
try {
throw new RuntimeException();
} catch (RuntimeException myException) {
}
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.blocks