Alex Rivera | Logout

Retry after Exception in Groovy

Asked 2011-08-17T01:55:25.613
18

In Ruby, I can write:

begin
  do_something # exception raised
rescue
   # handles error
   retry  # restart from beginning
end

Is there something similar in Groovy/Java?

I found this but maybe there is something better ?

Edit
Report

2 Answers

2

These days people will advise you to use ScheduledExecutorService to implement this kind of try-catch-retry functionality, as Thread.sleep() is considered outdated and potentially bad for performance. I was going to point you to a good answer on this by cletus, but can't for the life of me find it. I'll update my answer if I can dig it up.

EDIT: found it: How to retry function request after a certain time Hopefully this is of some help to you.

answered 2011-08-17T02:26:34.927
1

I can suggest to emulate kinda the same (I'm not sure about semantics of retry):

def retry(handler, c) {
    try {
        c()
    } catch(e) {
        handler(e)
        retry(handler, c)  // restart from beginning
    }
}

def handler = {e ->
    // handles error
}

retry(handler) {
    do_something // exception raised
}
answered 2011-08-17T02:31:31.370

Your Answer