Configuring exceptions to exclude from retry (using annotations)

In the previous post under Spring Retry, I showed with example how to specify the exceptions through annotation, for which a annotated operation has to be retried.

In this post, I will show with example how to mention the exceptions through annotation, that when thrown by a annotated operation, Spring retry shouldn’t retry that operation.

In other words, how to exclude the exceptions for which Spring retry shouldn’t retry the failed operation.

For this post, I will use the below service class

Service Class

1  import org.springframework.retry.annotation.Retryable;
2  import org.springframework.stereotype.Service;
4  @Service
5  public class Service9 {
6      private int i = 0;
8      @Retryable(exclude = {NullPointerException.class, IllegalArgumentException.class})
9      public void executeWithException() {
10         i = i + 1;
11         System.out.println("Executing method 'executeWithException' : " + i);
12         if (i == 1) {
13             throw new IndexOutOfBoundsException();
14         } else if (i == 2) {
15             throw new IllegalArgumentException();
16         } else {
17             throw new NullPointerException();
18         }
19     }
20 }

In the above code, at line 8, I annotate the “executeWithException” with “@Retryable” annotation.

We also set the annotations “exclude” attribute with list of exceptions that the Spring Retry has to ignore, when the annotated method throws those exceptions.

In our code, we are telling Spring retry to ignore NullPointerException and IllegalArgumentException.

Next I will show the main code for your reference.

Main Class

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

public class Example30 {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Example30.class);
        Service9 service9 = (Service9) context.getBean("service9");

        try {
        } catch(Exception excep) {

    public Service9 getService() {
        return new Service9();

Below is the output


[INFO] AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Sat Jun 11 09:39:24 IST 2022]; root of context hierarchy
Executing method 'executeWithException' : 1
Executing method 'executeWithException' : 2
    at Service9.executeWithException(
    at Service9$$FastClassBySpringCGLIB$$560f7444.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
    at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(
    at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(
    at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(
    at Service9$$EnhancerBySpringCGLIB$$e6685250.executeWithException(<generated>)
    at Example30.main(

As you can see from the output, when the first exception was thrown it was “IndexOutOfBoundsException” and the Spring Retry retried the operation. Whereas when the second exception “IllegalArgumentException” was thrown, Spring Retry didn’t retried the operation.

In this way we configure the exceptions that has to be excluded when thrown.


Press like button if you like the post. Also share your opinion regarding posts through comments. Also mention what other framework you want me to cover through comments.

Leave a Reply