Skip to content

Commit f240f84

Browse files
committed
Try#orThrow can declare checked exceptions for catching purposes
1 parent 4796989 commit f240f84

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ might need to be reworked, and subtyping is obviously no longer supported.
1414
- ***Breaking Change***: `Strong` is now called `Cartesian` to better reflect the type of strength
1515
- ***Breaking Change***: new Optic type hierarchy more faithfully encodes profunctor constraints on optics, new `Optic`
1616
type is now the supertype of `Lens` and `Iso`, and `lens` package has been moved to `optics`
17-
- ***Breaking Change***: Try and Either no longer preserve `Throwable` type since it was inherently not type-safe
18-
anyway; Try is therefore no longer a `Bifunctor`
17+
- ***Breaking Change***: `Try` and `Either` no longer preserve `Throwable` type since it was inherently not type-safe
18+
anyway; Try is therefore no longer a `Bifunctor`, and `orThrow` can be used to declare checked
19+
exceptions that could be caught by corresponding catch blocks
1920
- `IO` is now stack-safe, regardless of whether the composition nests linearly or recursively
2021

2122
### Added

src/main/java/com/jnape/palatable/lambda/adt/Try.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public final Throwable forfeit(Function<? super A, ? extends Throwable> fn) {
108108
*
109109
* @return possibly the success value
110110
*/
111-
public abstract A orThrow();
111+
public abstract <T extends Throwable> A orThrow() throws T;
112112

113113
/**
114114
* If this is a success, wrap the value in a {@link Maybe#just} and return it. Otherwise, return {@link

src/test/java/com/jnape/palatable/lambda/adt/TryTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.junit.Assert.assertEquals;
3737
import static org.junit.Assert.assertThat;
3838
import static org.junit.Assert.assertTrue;
39+
import static org.junit.Assert.fail;
3940
import static testsupport.matchers.LeftMatcher.isLeftThat;
4041

4142
@RunWith(Traits.class)
@@ -219,4 +220,17 @@ public void lazyZip() {
219220
throw new AssertionError();
220221
})).value());
221222
}
223+
224+
@Test
225+
public void orThrowCanStillThrowCheckedExceptions() {
226+
try {
227+
Try.trying(() -> {
228+
throw new RuntimeException();
229+
}).<IOException>orThrow();
230+
fail("Expected RuntimeException to be thrown, but nothing was");
231+
} catch (IOException ioException) {
232+
fail("Expected thrown exception to not be IOException, but merely proving it can still be caught");
233+
} catch (Exception expected) {
234+
}
235+
}
222236
}

0 commit comments

Comments
 (0)