val config = Config<String>().apply {
attempts = 10
}
val result: String = retry(config) {
// some code that can fail, possibly with an exception
}
The above will retry the code block, up to 10 time, until it completes without an exception. If within 10 attempts
it never passes a RetryExceededException
will be thrown.
Or you can use the Retry
class which is a retrying Try
. It won't throw an exception but results in either a Success
or Failure
instance with the appropriate value of the result or the exception:
val result = Retry {
// some code that can fail, possibly with an exception
}
if (result is Success) { } else { }
Kotlin introduced Result
and runCatching
which is effectively Try
support. To support this, with retrying,
retryCatching
was added:
val result = retryCatching {
// some code that can fail, possibly with an exception
}
if (result.isSuccess) { } else { }