Skip to content

Commit 1e37030

Browse files
committed
BugFix: Throw an IllegalArgumentException instead of ArithmeticException if the observable is empty
1 parent 1b0deef commit 1e37030

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3593,14 +3593,16 @@ public static Observable<Double> sumDoubles(Observable<Double> source) {
35933593

35943594
/**
35953595
* Returns an Observable that computes the average of all elements in the source Observable.
3596-
* For an empty source, it causes an ArithmeticException.
3596+
* For an empty source, it causes an IllegalArgumentException.
35973597
* <p>
35983598
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/average.png">
35993599
*
36003600
* @param source
36013601
* Source observable to compute the average of.
36023602
* @return an Observable emitting the averageof all the elements of the source Observable
36033603
* as its single item.
3604+
* @throws IllegalArgumentException
3605+
* if Observable sequence is empty.
36043606
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.average%28v=vs.103%29.aspx">MSDN: Observable.Average</a>
36053607
*/
36063608
public static Observable<Integer> average(Observable<Integer> source) {

rxjava-core/src/main/java/rx/operators/OperationAverage.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public Tuple2<Integer> call(Tuple2<Integer> accu, Integer next) {
4444
}).map(new Func1<Tuple2<Integer>, Integer>() {
4545
@Override
4646
public Integer call(Tuple2<Integer> result) {
47-
return result.current / result.count; // may throw DivisionByZero, this should be correct...
47+
if (result.count == 0) {
48+
throw new IllegalArgumentException("Sequence contains no elements");
49+
}
50+
return result.current / result.count;
4851
}
4952
});
5053
}
@@ -58,7 +61,10 @@ public Tuple2<Long> call(Tuple2<Long> accu, Long next) {
5861
}).map(new Func1<Tuple2<Long>, Long>() {
5962
@Override
6063
public Long call(Tuple2<Long> result) {
61-
return result.current / result.count; // may throw DivisionByZero, this should be correct...
64+
if (result.count == 0) {
65+
throw new IllegalArgumentException("Sequence contains no elements");
66+
}
67+
return result.current / result.count;
6268
}
6369
});
6470
}
@@ -73,7 +79,7 @@ public Tuple2<Float> call(Tuple2<Float> accu, Float next) {
7379
@Override
7480
public Float call(Tuple2<Float> result) {
7581
if (result.count == 0) {
76-
throw new ArithmeticException("divide by zero");
82+
throw new IllegalArgumentException("Sequence contains no elements");
7783
}
7884
return result.current / result.count;
7985
}
@@ -90,7 +96,7 @@ public Tuple2<Double> call(Tuple2<Double> accu, Double next) {
9096
@Override
9197
public Double call(Tuple2<Double> result) {
9298
if (result.count == 0) {
93-
throw new ArithmeticException("divide by zero");
99+
throw new IllegalArgumentException("Sequence contains no elements");
94100
}
95101
return result.current / result.count;
96102
}

rxjava-core/src/test/java/rx/operators/OperationAverageTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testEmptyAverage() throws Throwable {
5252
average(src).subscribe(w);
5353

5454
verify(w, never()).onNext(anyInt());
55-
verify(w, times(1)).onError(any(ArithmeticException.class));
55+
verify(w, times(1)).onError(isA(IllegalArgumentException.class));
5656
verify(w, never()).onCompleted();
5757
}
5858

@@ -73,7 +73,7 @@ public void testEmptyAverageLongs() throws Throwable {
7373
averageLongs(src).subscribe(wl);
7474

7575
verify(wl, never()).onNext(anyLong());
76-
verify(wl, times(1)).onError(any(ArithmeticException.class));
76+
verify(wl, times(1)).onError(isA(IllegalArgumentException.class));
7777
verify(wl, never()).onCompleted();
7878
}
7979

@@ -94,7 +94,7 @@ public void testEmptyAverageFloats() throws Throwable {
9494
averageFloats(src).subscribe(wf);
9595

9696
verify(wf, never()).onNext(anyFloat());
97-
verify(wf, times(1)).onError(any(ArithmeticException.class));
97+
verify(wf, times(1)).onError(isA(IllegalArgumentException.class));
9898
verify(wf, never()).onCompleted();
9999
}
100100

@@ -115,7 +115,7 @@ public void testEmptyAverageDoubles() throws Throwable {
115115
averageDoubles(src).subscribe(wd);
116116

117117
verify(wd, never()).onNext(anyDouble());
118-
verify(wd, times(1)).onError(any(ArithmeticException.class));
118+
verify(wd, times(1)).onError(isA(IllegalArgumentException.class));
119119
verify(wd, never()).onCompleted();
120120
}
121121
}

0 commit comments

Comments
 (0)