Configure CheckStyle rule for empty catch blocks

This commit configures a new CheckStyle rule that fails for empty
"catch" blocks, unless the exception is named "ignored" or "expected".

This also fixes the remaining instances missed by the previous commit.

Closes gh-35047
This commit is contained in:
Brian Clozel 2025-06-15 15:39:31 +02:00
parent 0d4dfb6c1f
commit af7758cbc7
8 changed files with 16 additions and 12 deletions

View File

@ -453,9 +453,9 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
ClassUtils.toClassArray(ifcs), targetClass.getClassLoader()); ClassUtils.toClassArray(ifcs), targetClass.getClassLoader());
targetMethod = ClassUtils.getMostSpecificMethod(targetMethod, compositeInterface); targetMethod = ClassUtils.getMostSpecificMethod(targetMethod, compositeInterface);
} }
// Implemented interfaces probably expose conflicting method signatures... catch (IllegalArgumentException ex) {
// Proceed with original target method. // Implemented interfaces probably expose conflicting method signatures...
catch (IllegalArgumentException ignored) { // Proceed with original target method.
} }
} }
} }
@ -478,7 +478,7 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
try { try {
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch); shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
} }
catch (ReflectionWorldException ignored) { catch (ReflectionWorldException ex) {
// Failed to introspect target method, probably because it has been loaded // Failed to introspect target method, probably because it has been loaded
// in a special ClassLoader. Let's try the declaring ClassLoader instead... // in a special ClassLoader. Let's try the declaring ClassLoader instead...
try { try {
@ -501,7 +501,7 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
try { try {
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch); shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
} }
catch (ReflectionWorldException ignored) { catch (ReflectionWorldException ex) {
// Could neither introspect the target class nor the proxy class -> // Could neither introspect the target class nor the proxy class ->
// let's try the original method's declaring class before we give up... // let's try the original method's declaring class before we give up...
try { try {

View File

@ -48,7 +48,7 @@ final class Target_Introspector {
} while (!c.getName().equals("java.lang.Object")); } while (!c.getName().equals("java.lang.Object"));
} }
} }
catch (Exception exception) { catch (Exception ignored) {
} }
return null; return null;
} }

View File

@ -73,8 +73,7 @@ class BridgeMethodResolver {
} finally { } finally {
is.close(); is.close();
} }
} catch (IOException ignored) { } catch (IOException ignored) {}
}
} }
return resolved; return resolved;
} }

View File

@ -439,7 +439,8 @@ public abstract class AbstractListenerWriteProcessor<T> implements Processor<T,
// ignore // ignore
} }
@Override @Override
public <T> void onError(AbstractListenerWriteProcessor<T> processor, Throwable ignored) { public <T> void onError(AbstractListenerWriteProcessor<T> processor, Throwable ex) {
// ignore
} }
@Override @Override
public <T> void onComplete(AbstractListenerWriteProcessor<T> processor) { public <T> void onComplete(AbstractListenerWriteProcessor<T> processor) {

View File

@ -116,7 +116,7 @@ public class FormContentFilter extends OncePerRequestFilter {
MediaType mediaType = MediaType.parseMediaType(contentType); MediaType mediaType = MediaType.parseMediaType(contentType);
return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType); return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType);
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ignored) {
} }
} }
return false; return false;

View File

@ -611,7 +611,8 @@ class ExceptionHandlerExceptionResolverTests {
@ExceptionHandler(SocketTimeoutException.class) @ExceptionHandler(SocketTimeoutException.class)
@ResponseStatus(code = HttpStatus.GATEWAY_TIMEOUT, reason = "gateway.timeout") @ResponseStatus(code = HttpStatus.GATEWAY_TIMEOUT, reason = "gateway.timeout")
public void handleException(SocketTimeoutException ignored) { public void handleException(SocketTimeoutException ex) {
} }
} }

View File

@ -9,7 +9,7 @@
<suppress files="(^(?!.+[\\/]src[\\/]main[\\/]java[\\/]).*)|(.*framework-docs.*)" checks="JavadocPackage" /> <suppress files="(^(?!.+[\\/]src[\\/]main[\\/]java[\\/]).*)|(.*framework-docs.*)" checks="JavadocPackage" />
<!-- Global: tests and test fixtures --> <!-- Global: tests and test fixtures -->
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="AnnotationLocation|AnnotationUseStyle|AtclauseOrder|AvoidNestedBlocks|FinalClass|HideUtilityClassConstructor|InnerTypeLast|JavadocStyle|JavadocType|JavadocVariable|LeftCurly|MultipleVariableDeclarations|NeedBraces|OneTopLevelClass|OuterTypeFilename|RequireThis|SpringCatch|SpringJavadoc|SpringNoThis|SpringDeprecatedCheck"/> <suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="AnnotationLocation|AnnotationUseStyle|AtclauseOrder|AvoidNestedBlocks|FinalClass|HideUtilityClassConstructor|InnerTypeLast|JavadocStyle|JavadocType|JavadocVariable|LeftCurly|MultipleVariableDeclarations|NeedBraces|OneTopLevelClass|OuterTypeFilename|RequireThis|SpringCatch|SpringJavadoc|SpringNoThis|SpringDeprecatedCheck|EmptyCatchBlock"/>
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="RegexpSinglelineJava" id="systemOutErrPrint"/> <suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="RegexpSinglelineJava" id="systemOutErrPrint"/>
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="SpringJUnit5" message="should not be public"/> <suppress files="[\\/]src[\\/](test|testFixtures)[\\/](java|java21)[\\/]" checks="SpringJUnit5" message="should not be public"/>
<suppress files="[\\/]src[\\/]test[\\/](java|java21)[\\/]org[\\/]springframework[\\/].+(Tests|Suite)" checks="IllegalImport" id="bannedJUnitJupiterImports"/> <suppress files="[\\/]src[\\/]test[\\/](java|java21)[\\/]org[\\/]springframework[\\/].+(Tests|Suite)" checks="IllegalImport" id="bannedJUnitJupiterImports"/>

View File

@ -39,6 +39,9 @@
</module> </module>
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck"/> <module name="com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck"/>
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.AvoidNestedBlocksCheck"/> <module name="com.puppycrawl.tools.checkstyle.checks.blocks.AvoidNestedBlocksCheck"/>
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck">
<property name="exceptionVariableName" value="expected|ignore"/>
</module>
<!-- Class Design --> <!-- Class Design -->
<module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck"/> <module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck"/>