title: Java: how to handle retries without copy-paste code?
body: <p>I have multiple cases when I have to deal retrial for DB and networking operations. Everywhere I do it I have the following type of code:</p>
  
  <pre><code>    for (int iteration = 1; ; iteration++) {
          try {
              data = doSomethingUseful(data);
  
              break;
          } catch (SomeException | AndAnotherException e) {
              if (iteration == helper.getNumberOfRetries()) {
                  throw e;
              } else {
                  errorReporter.reportError("Got following error for data = {}. Continue trying after delay...", data, e);
                  utilities.defaultDelayForIteration(iteration);
                  handleSpecificCase(data);
              }
          }
      }
  </code></pre>
  
  <p>The issue is that this code pattern is copy-pasted all over my classes. Which is really bad. I can't figure out how to get rid of this for-break-catch copy-paste pattern, since I usually get different exception to handle, I want to log data I failed on (usually also different ways).</p>
  
  <p>Is there a good way to avoid this copy-paste in Java 7?</p>
  
  <p><strong>Edit:</strong> I do use guice for dependency injection. I do have checked exceptions. There could be multiple variables instead of just one data and they are all of different type.</p>
  
  <p><strong>Edit2</strong>: AOP approach looks as the most promising for me.</p>