Skip to content
Advertisement

Spring Retry – Exception problem and retries

How can we catch two different exceptions (ex. from .lang and .io packages) in the same block of @Retryable method. One, we return an IOException and the other we retry the method.

@Retryable(value = {Exception.calss } ,maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String getInfo() {
    try {
        //here we have an executive code that may have an IOException
    } catch(Exception ex) {
        //And here i would catch the Exception 
        throw new Exception();
    }   
}

Advertisement

Answer

You can use the include parameter of the annotation to handle multiple various exceptions:

@Retryable(
    include = { IllegalAccessException.class, IOException.class }, 
    maxAttempts = 3, 
    backoff = @Backoff(delay = 3000))
public String getInfo() {
    // some code
}
Advertisement