Skip to content

Commit edd8aed

Browse files
JakeWhartonakarnokd
authored andcommitted
Remove checked exceptions from transformer interfaces. (#4710)
These functions are for transforming the stream shape, not doing work. Any operation that would throw a checked exception should happen inside the stream, not when shaping it.
1 parent 1124dc7 commit edd8aed

13 files changed

+17
-133
lines changed

src/main/java/io/reactivex/Completable.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,7 @@ public final Throwable blockingGet(long timeout, TimeUnit unit) {
942942
*/
943943
@SchedulerSupport(SchedulerSupport.NONE)
944944
public final Completable compose(CompletableTransformer transformer) {
945-
try {
946-
return wrap(transformer.apply(this));
947-
} catch (Throwable ex) {
948-
Exceptions.throwIfFatal(ex);
949-
throw ExceptionHelper.wrapOrThrow(ex);
950-
}
945+
return wrap(transformer.apply(this));
951946
}
952947

953948
/**

src/main/java/io/reactivex/CompletableTransformer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public interface CompletableTransformer {
2222
* Applies a function to the upstream Completable and returns a CompletableSource.
2323
* @param upstream the upstream Completable instance
2424
* @return the transformed CompletableSource instance
25-
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
26-
* into a RuntimeException
2725
*/
28-
CompletableSource apply(Completable upstream) throws Exception;
26+
CompletableSource apply(Completable upstream);
2927
}

src/main/java/io/reactivex/Flowable.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -6322,12 +6322,7 @@ public final <U> Single<U> collectInto(final U initialItem, BiConsumer<? super U
63226322
@BackpressureSupport(BackpressureKind.PASS_THROUGH)
63236323
@SchedulerSupport(SchedulerSupport.NONE)
63246324
public final <R> Flowable<R> compose(FlowableTransformer<T, R> composer) {
6325-
try {
6326-
return fromPublisher(composer.apply(this));
6327-
} catch (Throwable ex) {
6328-
Exceptions.throwIfFatal(ex);
6329-
throw ExceptionHelper.wrapOrThrow(ex);
6330-
}
6325+
return fromPublisher(composer.apply(this));
63316326
}
63326327

63336328
/**

src/main/java/io/reactivex/FlowableTransformer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public interface FlowableTransformer<Upstream, Downstream> {
2727
* optionally different element type.
2828
* @param upstream the upstream Flowable instance
2929
* @return the transformed Publisher instance
30-
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
31-
* into a RuntimeException
3230
*/
33-
Publisher<Downstream> apply(Flowable<Upstream> upstream) throws Exception;
31+
Publisher<Downstream> apply(Flowable<Upstream> upstream);
3432
}

src/main/java/io/reactivex/Maybe.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -2010,12 +2010,7 @@ public final <U> Maybe<U> cast(final Class<? extends U> clazz) {
20102010
*/
20112011
@SchedulerSupport(SchedulerSupport.NONE)
20122012
public final <R> Maybe<R> compose(MaybeTransformer<T, R> transformer) {
2013-
try {
2014-
return wrap(transformer.apply(this));
2015-
} catch (Throwable ex) {
2016-
Exceptions.throwIfFatal(ex);
2017-
throw ExceptionHelper.wrapOrThrow(ex);
2018-
}
2013+
return wrap(transformer.apply(this));
20192014
}
20202015

20212016
/**

src/main/java/io/reactivex/MaybeTransformer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public interface MaybeTransformer<Upstream, Downstream> {
2525
* optionally different element type.
2626
* @param upstream the upstream Maybe instance
2727
* @return the transformed MaybeSource instance
28-
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
29-
* into a RuntimeException
3028
*/
31-
MaybeSource<Downstream> apply(Maybe<Upstream> upstream) throws Exception;
29+
MaybeSource<Downstream> apply(Maybe<Upstream> upstream);
3230
}

src/main/java/io/reactivex/Observable.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -5531,15 +5531,9 @@ public final <U> Single<U> collectInto(final U initialValue, BiConsumer<? super
55315531
*/
55325532
@SchedulerSupport(SchedulerSupport.NONE)
55335533
public final <R> Observable<R> compose(ObservableTransformer<T, R> composer) {
5534-
try {
5535-
return wrap(composer.apply(this));
5536-
} catch (Throwable ex) {
5537-
Exceptions.throwIfFatal(ex);
5538-
throw ExceptionHelper.wrapOrThrow(ex);
5539-
}
5534+
return wrap(composer.apply(this));
55405535
}
55415536

5542-
55435537
/**
55445538
* Returns a new Observable that emits items resulting from applying a function that you supply to each item
55455539
* emitted by the source ObservableSource, where that function returns an ObservableSource, and then emitting the items

src/main/java/io/reactivex/ObservableTransformer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public interface ObservableTransformer<Upstream, Downstream> {
2525
* optionally different element type.
2626
* @param upstream the upstream Observable instance
2727
* @return the transformed ObservableSource instance
28-
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
29-
* into a RuntimeException
3028
*/
31-
ObservableSource<Downstream> apply(Observable<Upstream> upstream) throws Exception;
29+
ObservableSource<Downstream> apply(Observable<Upstream> upstream);
3230
}

src/main/java/io/reactivex/Single.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1473,12 +1473,7 @@ public final Single<T> hide() {
14731473
*/
14741474
@SchedulerSupport(SchedulerSupport.NONE)
14751475
public final <R> Single<R> compose(SingleTransformer<T, R> transformer) {
1476-
try {
1477-
return wrap(transformer.apply(this));
1478-
} catch (Throwable ex) {
1479-
Exceptions.throwIfFatal(ex);
1480-
throw ExceptionHelper.wrapOrThrow(ex);
1481-
}
1476+
return wrap(transformer.apply(this));
14821477
}
14831478

14841479
/**

src/main/java/io/reactivex/SingleTransformer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public interface SingleTransformer<Upstream, Downstream> {
2525
* optionally different element type.
2626
* @param upstream the upstream Single instance
2727
* @return the transformed SingleSource instance
28-
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
29-
* into a RuntimeException
3028
*/
31-
SingleSource<Downstream> apply(Single<Upstream> upstream) throws Exception;
29+
SingleSource<Downstream> apply(Single<Upstream> upstream);
3230
}

src/test/java/io/reactivex/TransformerTest.java

+5-85
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void flowableTransformerThrows() {
2929
try {
3030
Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() {
3131
@Override
32-
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
32+
public Publisher<Integer> apply(Flowable<Integer> v) {
3333
throw new TestException("Forced failure");
3434
}
3535
});
@@ -39,28 +39,12 @@ public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
3939
}
4040
}
4141

42-
@Test
43-
public void flowableTransformerThrowsChecked() {
44-
try {
45-
Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() {
46-
@Override
47-
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
48-
throw new IOException("Forced failure");
49-
}
50-
});
51-
fail("Should have thrown!");
52-
} catch (RuntimeException ex) {
53-
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
54-
assertEquals("Forced failure", ex.getCause().getMessage());
55-
}
56-
}
57-
5842
@Test
5943
public void observableTransformerThrows() {
6044
try {
6145
Observable.just(1).compose(new ObservableTransformer<Integer, Integer>() {
6246
@Override
63-
public Observable<Integer> apply(Observable<Integer> v) throws Exception {
47+
public Observable<Integer> apply(Observable<Integer> v) {
6448
throw new TestException("Forced failure");
6549
}
6650
});
@@ -70,28 +54,12 @@ public Observable<Integer> apply(Observable<Integer> v) throws Exception {
7054
}
7155
}
7256

73-
@Test
74-
public void observableTransformerThrowsChecked() {
75-
try {
76-
Observable.just(1).compose(new ObservableTransformer<Integer, Integer>() {
77-
@Override
78-
public Observable<Integer> apply(Observable<Integer> v) throws Exception {
79-
throw new IOException("Forced failure");
80-
}
81-
});
82-
fail("Should have thrown!");
83-
} catch (RuntimeException ex) {
84-
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
85-
assertEquals("Forced failure", ex.getCause().getMessage());
86-
}
87-
}
88-
8957
@Test
9058
public void singleTransformerThrows() {
9159
try {
9260
Single.just(1).compose(new SingleTransformer<Integer, Integer>() {
9361
@Override
94-
public Single<Integer> apply(Single<Integer> v) throws Exception {
62+
public Single<Integer> apply(Single<Integer> v) {
9563
throw new TestException("Forced failure");
9664
}
9765
});
@@ -101,28 +69,12 @@ public Single<Integer> apply(Single<Integer> v) throws Exception {
10169
}
10270
}
10371

104-
@Test
105-
public void singleTransformerThrowsChecked() {
106-
try {
107-
Single.just(1).compose(new SingleTransformer<Integer, Integer>() {
108-
@Override
109-
public Single<Integer> apply(Single<Integer> v) throws Exception {
110-
throw new IOException("Forced failure");
111-
}
112-
});
113-
fail("Should have thrown!");
114-
} catch (RuntimeException ex) {
115-
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
116-
assertEquals("Forced failure", ex.getCause().getMessage());
117-
}
118-
}
119-
12072
@Test
12173
public void maybeTransformerThrows() {
12274
try {
12375
Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() {
12476
@Override
125-
public Maybe<Integer> apply(Maybe<Integer> v) throws Exception {
77+
public Maybe<Integer> apply(Maybe<Integer> v) {
12678
throw new TestException("Forced failure");
12779
}
12880
});
@@ -132,28 +84,12 @@ public Maybe<Integer> apply(Maybe<Integer> v) throws Exception {
13284
}
13385
}
13486

135-
@Test
136-
public void maybeTransformerThrowsChecked() {
137-
try {
138-
Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() {
139-
@Override
140-
public Maybe<Integer> apply(Maybe<Integer> v) throws Exception {
141-
throw new IOException("Forced failure");
142-
}
143-
});
144-
fail("Should have thrown!");
145-
} catch (RuntimeException ex) {
146-
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
147-
assertEquals("Forced failure", ex.getCause().getMessage());
148-
}
149-
}
150-
15187
@Test
15288
public void completabeTransformerThrows() {
15389
try {
15490
Completable.complete().compose(new CompletableTransformer() {
15591
@Override
156-
public Completable apply(Completable v) throws Exception {
92+
public Completable apply(Completable v) {
15793
throw new TestException("Forced failure");
15894
}
15995
});
@@ -162,20 +98,4 @@ public Completable apply(Completable v) throws Exception {
16298
assertEquals("Forced failure", ex.getMessage());
16399
}
164100
}
165-
166-
@Test
167-
public void completabeTransformerThrowsChecked() {
168-
try {
169-
Completable.complete().compose(new CompletableTransformer() {
170-
@Override
171-
public Completable apply(Completable v) throws Exception {
172-
throw new IOException("Forced failure");
173-
}
174-
});
175-
fail("Should have thrown!");
176-
} catch (RuntimeException ex) {
177-
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
178-
assertEquals("Forced failure", ex.getCause().getMessage());
179-
}
180-
}
181101
}

src/test/java/io/reactivex/internal/operators/single/SingleMiscTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void compose() {
8585
Single.just(1)
8686
.compose(new SingleTransformer<Integer, Object>() {
8787
@Override
88-
public SingleSource<Object> apply(Single<Integer> f) throws Exception {
88+
public SingleSource<Object> apply(Single<Integer> f) {
8989
return f.map(new Function<Integer, Object>() {
9090
@Override
9191
public Object apply(Integer v) throws Exception {

src/test/java/io/reactivex/maybe/MaybeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void toNull() {
390390
public void compose() {
391391
Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() {
392392
@Override
393-
public MaybeSource<Integer> apply(Maybe<Integer> m) throws Exception {
393+
public MaybeSource<Integer> apply(Maybe<Integer> m) {
394394
return m.map(new Function<Integer, Integer>() {
395395
@Override
396396
public Integer apply(Integer w) throws Exception {

0 commit comments

Comments
 (0)