Groovy web console

subscribe to the feed Subscribe
to this
site

retry

Published 2 weeks ago by okor with tags retry
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
e = new Exception("oh no")
e2 = new Exception(e)



  public static retry(int max_retries = 3, Closure body) {
    int retries = 0
    while(retries++ < max_retries) {
      if (retries > 0) {
        println "Retry count $retries of $max_retries"
      }
      try {
        return body.call()
      } catch(e) {
        if (retries == max_retries) {
          throw e
        } else {
          println(e.message)
        }
      }
    }
  }


def throwe(e) {
    throw e;
}

retry {
    throwe(e)
}

retry (5) {
    throwe(e)
}