@Test public void noRetriesForOtherExceptions() throws IOException { doThrow(new NullPointerException()).doNothing().when(task).run(); try { tryTimes(3, task, null); fail("An exception should have been thrown"); } catch (NullPointerException e) { verify(task, times(1)).run(); } }
public static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler) throws UncheckedIOException, IllegalArgumentException { if (times < 1) { throw new IllegalArgumentException("The number of retries should be a positive integer. It was " + times); } int retries = 0; while (true) { try { task.run(); return; } catch (IOException e) { onError(errorHandler, e); if (++retries == times) { throw new UncheckedIOException( format("Operation failed after %d retries: %s", times, e.getMessage()), e); } } } }
IoRetry { public static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler) throws UncheckedIOException, IllegalArgumentException { if (times < 1) { throw new IllegalArgumentException("The number of retries should be a positive integer. It was " + times); } int retries = 0; while (true) { try { task.run(); return; } catch (IOException e) { onError(errorHandler, e); if (++retries == times) { throw new UncheckedIOException( format("Operation failed after %d retries: %s", times, e.getMessage()), e); } } } } }
IoRetry { public static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler) throws UncheckedIOException, IllegalArgumentException { if (times < 1) { throw new IllegalArgumentException("The number of retries should be a positive integer. It was " + times); } int retries = 0; while (true) { try { task.run(); return; } catch (IOException e) { onError(errorHandler, e); if (++retries == times) { throw new UncheckedIOException( format("Operation failed after %d retries: %s", times, e.getMessage()), e); } } } } private  IoRetry(); }
IoRetry { public static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler) throws UncheckedIOException, IllegalArgumentException { if (times < 1) { throw new IllegalArgumentException("The number of retries should be a positive integer. It was " + times); } int retries = 0; while (true) { try { task.run(); return; } catch (IOException e) { onError(errorHandler, e); if (++retries == times) { throw new UncheckedIOException( format("Operation failed after %d retries: %s", times, e.getMessage()), e); } } } } private  IoRetry(); static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler); }
IoRetry { public static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler) throws UncheckedIOException, IllegalArgumentException { if (times < 1) { throw new IllegalArgumentException("The number of retries should be a positive integer. It was " + times); } int retries = 0; while (true) { try { task.run(); return; } catch (IOException e) { onError(errorHandler, e); if (++retries == times) { throw new UncheckedIOException( format("Operation failed after %d retries: %s", times, e.getMessage()), e); } } } } private  IoRetry(); static void tryTimes(int times, IOAction task, Consumer<IOException> errorHandler);  }