Rename exception variables in empty catch blocks

The Spring codebase sometimes ignores exceptions in catch blocks on
purpose. This is often called out by an inline comment.
We should make this more obvious by renaming the exception argument in
the catch block to declare whether the exception is "ignored" or
"expected".

See gh-35047

Signed-off-by: Vincent Potucek <vpotucek@me.com>
[brian.clozel@broadcom.com: rework commit message]
Signed-off-by: Brian Clozel <brian.clozel@broadcom.com>
This commit is contained in:
Vincent Potucek 2025-06-13 16:03:24 +02:00 committed by Brian Clozel
parent cd3ac44fb0
commit 0d4dfb6c1f
46 changed files with 66 additions and 108 deletions

View File

@ -158,8 +158,7 @@ class AdvisorAutoProxyCreatorIntegrationTests {
try { try {
rb.echoException(new ServletException()); rb.echoException(new ServletException());
} }
catch (ServletException ex) { catch (ServletException ignored) {
} }
assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1); assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
} }
@ -272,7 +271,7 @@ class OrderedTxCheckAdvisor extends StaticMethodMatcherPointcutAdvisor implement
TransactionInterceptor.currentTransactionStatus(); TransactionInterceptor.currentTransactionStatus();
throw new RuntimeException("Shouldn't have a transaction"); throw new RuntimeException("Shouldn't have a transaction");
} }
catch (NoTransactionException ex) { catch (NoTransactionException ignored) {
// this is Ok // this is Ok
} }
} }

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);
} }
catch (IllegalArgumentException ex) { // Implemented interfaces probably expose conflicting method signatures...
// Implemented interfaces probably expose conflicting method signatures... // Proceed with original target method.
// Proceed with original target method. catch (IllegalArgumentException ignored) {
} }
} }
} }
@ -478,7 +478,7 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
try { try {
shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch); shadowMatch = pointcutExpression.matchesMethodExecution(methodToMatch);
} }
catch (ReflectionWorldException ex) { catch (ReflectionWorldException ignored) {
// 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 ex) { catch (ReflectionWorldException ignored) {
// 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

@ -208,8 +208,8 @@ public abstract class BeanFactoryAnnotationUtils {
} }
} }
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ignored) {
// Ignore - can't compare qualifiers for a manually registered singleton object // can't compare qualifiers for a manually registered singleton object
} }
} }
return false; return false;

View File

@ -523,8 +523,7 @@ final class PostProcessorRegistrationDelegate {
try { try {
typedStringValue.resolveTargetType(this.beanFactory.getBeanClassLoader()); typedStringValue.resolveTargetType(this.beanFactory.getBeanClassLoader());
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ignored) {
// ignore
} }
} }

View File

@ -266,8 +266,8 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class); Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class);
ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader()); ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader());
} }
catch (NoSuchMethodException ex) { catch (NoSuchMethodException ignored) {
// Ignore - no Hibernate Validator 5.2+ or similar provider // no Hibernate Validator 5.2+ or similar provider
} }
} }
@ -417,8 +417,8 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
try { try {
return super.unwrap(type); return super.unwrap(type);
} }
catch (ValidationException ex) { catch (ValidationException ignored) {
// Ignore - we'll try ValidatorFactory unwrapping next // we'll try ValidatorFactory unwrapping next
} }
} }
if (this.validatorFactory != null) { if (this.validatorFactory != null) {

View File

@ -233,8 +233,7 @@ abstract class AbstractAopProxyTests {
try { try {
p2.echo(new IOException()); p2.echo(new IOException());
} }
catch (IOException ex) { catch (IOException ignored) {
} }
assertThat(cta.getCalls()).isEqualTo(2); assertThat(cta.getCalls()).isEqualTo(2);
} }

View File

@ -84,8 +84,7 @@ final class CompileWithForkedClassLoaderClassLoader extends ClassLoader {
try (stream) { try (stream) {
return stream.readAllBytes(); return stream.readAllBytes();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }
return null; return null;

View File

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

View File

@ -62,8 +62,8 @@ public class MethodProxy {
try { try {
proxy.init(); proxy.init();
} }
catch (CodeGenerationException ex) { catch (CodeGenerationException ignored) {
// Ignore - to be retried when actually needed later on (possibly not at all) // to be retried when actually needed later on (possibly not at all)
} }
} }
// SPRING PATCH END // SPRING PATCH END

View File

@ -438,8 +438,7 @@ public class SpringFactoriesLoader {
return constructor; return constructor;
} }
} }
catch (UnsupportedOperationException ex) { catch (UnsupportedOperationException ignored) {
// ignore
} }
return null; return null;
} }

View File

@ -229,8 +229,7 @@ public abstract class FileCopyUtils {
try { try {
closeable.close(); closeable.close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -526,8 +526,7 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
try { try {
conn.close(); conn.close();
} }
catch (Throwable ex) { catch (Throwable ignored) {
// ignore
} }
} }
} }

View File

@ -1000,8 +1000,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
URI uri = ResourceUtils.toURI(elementNamespace); URI uri = ResourceUtils.toURI(elementNamespace);
return uri.getHost(); return uri.getHost();
} }
catch (URISyntaxException ex) { catch (URISyntaxException ignored) {
// ignore
} }
return dataHandler.getName(); return dataHandler.getName();
} }

View File

@ -710,8 +710,7 @@ class R2dbcTransactionManagerTests {
try { try {
return Mono.fromRunnable(() -> doAfterCompletion(status)); return Mono.fromRunnable(() -> doAfterCompletion(status));
} }
catch (Throwable ex) { catch (Throwable ignored) {
// ignore
} }
return Mono.empty(); return Mono.empty();

View File

@ -100,8 +100,7 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
try { try {
getBody().close(); getBody().close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -1129,8 +1129,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
try { try {
return simpleDateFormat.parse(value).getTime(); return simpleDateFormat.parse(value).getTime();
} }
catch (ParseException ex) { catch (ParseException ignored) {
// ignore
} }
} }
throw new IllegalArgumentException("Cannot parse date value '" + value + "' for '" + name + "' header"); throw new IllegalArgumentException("Cannot parse date value '" + value + "' for '" + name + "' header");

View File

@ -1223,8 +1223,7 @@ public class HttpHeaders implements Serializable {
try { try {
port = Integer.parseInt(portString); port = Integer.parseInt(portString);
} }
catch (NumberFormatException ex) { catch (NumberFormatException ignored) {
// ignore
} }
} }

View File

@ -89,8 +89,7 @@ final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
this.httpResponse.close(); this.httpResponse.close();
} }
} }
catch (IOException ex) { catch (IOException ignored) {
// Ignore exception on close...
} }
} }

View File

@ -108,8 +108,7 @@ final class ReactorClientHttpResponse implements ClientHttpResponse {
StreamUtils.drain(body); StreamUtils.drain(body);
body.close(); body.close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -119,8 +119,7 @@ class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
ZonedDateTime expiresDate = ZonedDateTime.parse(expiresAttribute, DateTimeFormatter.RFC_1123_DATE_TIME); ZonedDateTime expiresDate = ZonedDateTime.parse(expiresAttribute, DateTimeFormatter.RFC_1123_DATE_TIME);
return Duration.between(ZonedDateTime.now(expiresDate.getZone()), expiresDate).toSeconds(); return Duration.between(ZonedDateTime.now(expiresDate.getZone()), expiresDate).toSeconds();
} }
catch (DateTimeParseException ex) { catch (DateTimeParseException ignored) {
// ignore
} }
} }
return -1; return -1;

View File

@ -158,20 +158,20 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
in.transferTo(out); in.transferTo(out);
out.flush(); out.flush();
} }
catch (NullPointerException ex) { catch (NullPointerException ignored) {
// ignore, see SPR-13620 // see SPR-13620
} }
finally { finally {
try { try {
in.close(); in.close();
} }
catch (Throwable ex) { catch (Throwable ignored) {
// ignore, see SPR-12999 // see SPR-12999
} }
} }
} }
catch (FileNotFoundException ex) { catch (FileNotFoundException ignored) {
// ignore, see SPR-12999 // see SPR-12999
} }
} }

View File

@ -183,8 +183,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
try { try {
in.close(); in.close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }
} }
@ -244,8 +243,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
in.close(); in.close();
} }
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -270,8 +270,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
transform(t, new StreamResult(os)); transform(t, new StreamResult(os));
return os.count; return os.count;
} }
catch (TransformerException ex) { catch (TransformerException ignored) {
// ignore
} }
} }
return null; return null;

View File

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

View File

@ -309,8 +309,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
try { try {
listener.onTimeout(event); listener.onTimeout(event);
} }
catch (Exception ex) { catch (Exception ignored) {
// Ignore
} }
} }
@ -318,8 +317,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
try { try {
listener.onError(event); listener.onError(event);
} }
catch (Exception ex) { catch (Exception ignored) {
// Ignore
} }
} }
@ -327,8 +325,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
try { try {
listener.onComplete(event); listener.onComplete(event);
} }
catch (Exception ex) { catch (Exception ignored) {
// Ignore
} }
} }

View File

@ -152,8 +152,7 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
try { try {
return FileCopyUtils.copyToByteArray(response.getBody()); return FileCopyUtils.copyToByteArray(response.getBody());
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
return new byte[0]; return new byte[0];
} }

View File

@ -113,8 +113,7 @@ class CallableInterceptorChain {
try { try {
future.cancel(true); future.cancel(true);
} }
catch (Throwable ex) { catch (Throwable ignored) {
// Ignore
} }
} }
} }

View File

@ -235,8 +235,7 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements
break; break;
} }
} }
catch (InterruptedException ex) { catch (InterruptedException ignored) {
// ignore
} }
} }

View File

@ -122,8 +122,7 @@ public class ServletContextResource extends AbstractFileResolvingResource implem
try { try {
is.close(); is.close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
return true; return true;
} }

View File

@ -151,8 +151,7 @@ public final class HttpServiceProxyRegistryFactoryBean
Class<?> clazz = ClassUtils.forName(className, HttpServiceGroupAdapter.class.getClassLoader()); Class<?> clazz = ClassUtils.forName(className, HttpServiceGroupAdapter.class.getClassLoader());
groupAdapters.put(clientType, (HttpServiceGroupAdapter<?>) BeanUtils.instantiateClass(clazz)); groupAdapters.put(clientType, (HttpServiceGroupAdapter<?>) BeanUtils.instantiateClass(clazz));
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ignored) {
// ignore
} }
} }
} }

View File

@ -93,7 +93,7 @@ class RequestContextListenerTests {
try { try {
thread.join(); thread.join();
} }
catch (InterruptedException ex) { catch (InterruptedException ignored) {
} }
// Still bound to original thread, but at least completed. // Still bound to original thread, but at least completed.
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull(); assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();

View File

@ -100,8 +100,7 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
try { try {
getBody().close(); getBody().close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -1128,8 +1128,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
try { try {
return simpleDateFormat.parse(value).getTime(); return simpleDateFormat.parse(value).getTime();
} }
catch (ParseException ex) { catch (ParseException ignored) {
// ignore
} }
} }
throw new IllegalArgumentException("Cannot parse date value '" + value + "' for '" + name + "' header"); throw new IllegalArgumentException("Cannot parse date value '" + value + "' for '" + name + "' header");

View File

@ -77,8 +77,7 @@ abstract class WebClientUtils {
try { try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null); uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
} }
catch (URISyntaxException ex) { catch (URISyntaxException ignored) {
// ignore
} }
} }
return httpMethod.name() + " " + uri; return httpMethod.name() + " " + uri;

View File

@ -357,8 +357,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
.cache(); .cache();
} }
} }
catch (InvalidMediaTypeException ex) { catch (InvalidMediaTypeException ignored) {
// Ignore
} }
return EMPTY_FORM_DATA; return EMPTY_FORM_DATA;
} }
@ -379,8 +378,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
.cache(); .cache();
} }
} }
catch (InvalidMediaTypeException ex) { catch (InvalidMediaTypeException ignored) {
// Ignore
} }
return EMPTY_MULTIPART_DATA; return EMPTY_MULTIPART_DATA;
} }

View File

@ -76,8 +76,7 @@ public abstract class ResourceHandlerUtils {
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR), Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path); "Resource location does not end with slash: " + path);
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -211,8 +211,7 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
return EMPTY_CONDITION; return EMPTY_CONDITION;
} }
} }
catch (NotAcceptableStatusException | UnsupportedMediaTypeStatusException ex) { catch (NotAcceptableStatusException | UnsupportedMediaTypeStatusException ignored) {
// Ignore
} }
} }
return null; return null;

View File

@ -622,8 +622,7 @@ public class MvcUriComponentsBuilder {
try { try {
return wac.getBean(MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME, CompositeUriComponentsContributor.class); return wac.getBean(MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME, CompositeUriComponentsContributor.class);
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ignored) {
// Ignore
} }
} }
return defaultUriComponentsContributor; return defaultUriComponentsContributor;

View File

@ -788,8 +788,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
return getBeanFactory().getBean( return getBeanFactory().getBean(
DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class); DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class);
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ignored) {
// ignore
} }
} }
return null; return null;

View File

@ -191,8 +191,7 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
try { try {
body.close(); body.close();
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -77,8 +77,7 @@ public abstract class ResourceHandlerUtils {
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR), Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path); "Resource location does not end with slash: " + path);
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -611,8 +611,7 @@ 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 ex) { public void handleException(SocketTimeoutException ignored) {
} }
} }

View File

@ -41,8 +41,7 @@ public class BinaryWebSocketHandler extends AbstractWebSocketHandler {
try { try {
session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Text messages not supported")); session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Text messages not supported"));
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -93,8 +93,7 @@ public class ExceptionWebSocketHandlerDecorator extends WebSocketHandlerDecorato
try { try {
session.close(CloseStatus.SERVER_ERROR); session.close(CloseStatus.SERVER_ERROR);
} }
catch (Throwable ex) { catch (Throwable ignored) {
// ignore
} }
} }
} }

View File

@ -41,8 +41,7 @@ public class TextWebSocketHandler extends AbstractWebSocketHandler {
try { try {
session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Binary messages not supported")); session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Binary messages not supported"));
} }
catch (IOException ex) { catch (IOException ignored) {
// ignore
} }
} }

View File

@ -412,8 +412,7 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
try { try {
session.close(CloseStatus.PROTOCOL_ERROR); session.close(CloseStatus.PROTOCOL_ERROR);
} }
catch (IOException ex) { catch (IOException ignored) {
// Ignore
} }
} }
} }