Skip to content

Commit bc06b31

Browse files
committed
Change the checked functional interfaces to throw Throwable
This commit changes the checked functional interfaces to throw Throwable rather than Exception. The reasons for this are: - It allows execution failures, such as in ExecutionAttemptedEvent, to be propagated (rethrown) such as in a Fallback, and execution failures are already typed as Throwables. - Helps unify the various failure handling APIs (in FailurePolicy), which currently accept Throwables. - Aligns with precedent in CompletableFuture, which accepts Throwables (via completeExceptionally).
1 parent 709c2ed commit bc06b31

File tree

10 files changed

+18
-16
lines changed

10 files changed

+18
-16
lines changed

src/main/java/net/jodah/failsafe/CircuitBreaker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private void transitionTo(State newState, CheckedRunnable listener, ExecutionCon
434434
if (transitioned && listener != null) {
435435
try {
436436
listener.run();
437-
} catch (Exception ignore) {
437+
} catch (Throwable ignore) {
438438
}
439439
}
440440
}

src/main/java/net/jodah/failsafe/Fallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ public boolean isAsync() {
214214
/**
215215
* Returns the applied fallback result.
216216
*/
217-
R apply(R result, Throwable failure, ExecutionContext context) throws Exception {
217+
R apply(R result, Throwable failure, ExecutionContext context) throws Throwable {
218218
ExecutionAttemptedEvent<R> event = new ExecutionAttemptedEvent<>(result, failure, context);
219219
return fallback != null ? fallback.apply(event) : fallbackStage.apply(event).get();
220220
}
221221

222222
/**
223223
* Returns a future applied fallback result.
224224
*/
225-
CompletableFuture<R> applyStage(R result, Throwable failure, ExecutionContext context) throws Exception {
225+
CompletableFuture<R> applyStage(R result, Throwable failure, ExecutionContext context) throws Throwable {
226226
ExecutionAttemptedEvent<R> event = new ExecutionAttemptedEvent<>(result, failure, context);
227227
return fallback != null ? CompletableFuture.completedFuture(fallback.apply(event)) : fallbackStage.apply(event);
228228
}

src/main/java/net/jodah/failsafe/FallbackExecutor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ protected ExecutionResult onFailure(ExecutionResult result) {
3434
return policy == Fallback.VOID ?
3535
result.withNonResult() :
3636
result.withResult(policy.apply(result.getResult(), result.getFailure(), execution.copy()));
37-
} catch (Exception e) {
38-
return ExecutionResult.failure(e);
37+
} catch (Throwable t) {
38+
return ExecutionResult.failure(t);
3939
}
4040
}
4141

@@ -53,8 +53,8 @@ protected CompletableFuture<ExecutionResult> onFailureAsync(ExecutionResult resu
5353
ExecutionResult r = failure == null ? result.withResult(innerResult) : ExecutionResult.failure(failure);
5454
promise.complete(r);
5555
});
56-
} catch (Exception e) {
57-
promise.complete(ExecutionResult.failure(e));
56+
} catch (Throwable t) {
57+
promise.complete(ExecutionResult.failure(t));
5858
}
5959
return null;
6060
};

src/main/java/net/jodah/failsafe/function/CheckedConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
*/
2424
@FunctionalInterface
2525
public interface CheckedConsumer<T> {
26-
void accept(T t) throws Exception;
26+
void accept(T t) throws Throwable;
2727
}

src/main/java/net/jodah/failsafe/function/CheckedFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
*/
2424
@FunctionalInterface
2525
public interface CheckedFunction<T, R> {
26-
R apply(T t) throws Exception;
26+
R apply(T t) throws Throwable;
2727
}

src/main/java/net/jodah/failsafe/function/CheckedRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
*/
2323
@FunctionalInterface
2424
public interface CheckedRunnable {
25-
void run() throws Exception;
25+
void run() throws Throwable;
2626
}

src/main/java/net/jodah/failsafe/function/CheckedSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
*/
2424
@FunctionalInterface
2525
public interface CheckedSupplier<T> {
26-
T get() throws Exception;
26+
T get() throws Throwable;
2727
}

src/main/java/net/jodah/failsafe/function/ContextualRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
*/
2525
@FunctionalInterface
2626
public interface ContextualRunnable {
27-
void run(ExecutionContext context) throws Exception;
27+
void run(ExecutionContext context) throws Throwable;
2828
}

src/main/java/net/jodah/failsafe/function/ContextualSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
*/
2626
@FunctionalInterface
2727
public interface ContextualSupplier<T> {
28-
T get(ExecutionContext context) throws Exception;
28+
T get(ExecutionContext context) throws Throwable;
2929
}

src/test/java/net/jodah/failsafe/Testing.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Throwable getThrowable(CheckedRunnable runnable) {
5050
public static <T> T ignoreExceptions(CheckedSupplier<T> supplier) {
5151
try {
5252
return supplier.get();
53-
} catch (Exception e) {
53+
} catch (Throwable t) {
5454
return null;
5555
}
5656
}
@@ -116,8 +116,10 @@ public static <T> T unwrapExceptions(CheckedSupplier<T> supplier) {
116116
} catch (FailsafeException e) {
117117
sneakyThrow(e.getCause() == null ? e : e.getCause());
118118
return null;
119-
} catch (Exception e) {
120-
throw (RuntimeException) e;
119+
} catch (RuntimeException | Error e) {
120+
throw e;
121+
} catch (Throwable t) {
122+
throw new RuntimeException(t);
121123
}
122124
}
123125

0 commit comments

Comments
 (0)