Configuring Exceptions to include for retry (using annotations)

In this post under Spring Retry, I will show with example how to configure the exceptions for which retry has to happen.

For the example I will use the below service class.


1  import org.springframework.retry.annotation.Retryable;
2  import org.springframework.stereotype.Service;
4  @Service
5  public class Service8 {
6      private int i = 0;
8      @Retryable(value = {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 NullPointerException();
14         } else if (i == 2) {
15             throw new IllegalArgumentException();
16         } else {
17             throw new IndexOutOfBoundsException();
18         }
19     }
20 }

As you can see in the above code, the method to be retried is annotated with “Retryable” annotation. The annotation also list the exceptions for which the method has to be retried separated by comma. In this way we can set the exceptions for which the method has to be retried.

Next we will see the main class code for your reference.


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 Example29 {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Example29.class);
        Service8 service8 = (Service8) context.getBean("service8");

        try {
        } catch(Exception excep) {

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

Below is the output


[INFO] AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Sat Jun 04 09:33:06 IST 2022]; root of context hierarchy
Executing method 'executeWithException' : 1
Executing method 'executeWithException' : 2
Executing method 'executeWithException' : 3
    at Service8.executeWithException(
    at Service8$$FastClassBySpringCGLIB$$560f7443.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 Service8$$EnhancerBySpringCGLIB$$f805f537.executeWithException(<generated>)
    at Example29.main(

If you see the output, “executeWithException” is retried whenever “NullPointerException” and “IllegalArgumentException” is thrown

But when “IndexOutOfBoundsException” is thrown, the method is not retried.


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