diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index ae3796a8d5..73a75fa479 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -3596,7 +3596,7 @@ public static Observable sumDoubles(Observable source) { /** * Returns an Observable that computes the average of all elements in the source Observable. - * For an empty source, it causes an ArithmeticException. + * For an empty source, it causes an IllegalArgumentException. *

* * @@ -3604,6 +3604,8 @@ public static Observable sumDoubles(Observable source) { * Source observable to compute the average of. * @return an Observable emitting the averageof all the elements of the source Observable * as its single item. + * @throws IllegalArgumentException + * if Observable sequence is empty. * @see MSDN: Observable.Average */ public static Observable average(Observable source) { diff --git a/rxjava-core/src/main/java/rx/operators/OperationAverage.java b/rxjava-core/src/main/java/rx/operators/OperationAverage.java index 3fbbd1f05e..35abc99eb5 100644 --- a/rxjava-core/src/main/java/rx/operators/OperationAverage.java +++ b/rxjava-core/src/main/java/rx/operators/OperationAverage.java @@ -44,7 +44,10 @@ public Tuple2 call(Tuple2 accu, Integer next) { }).map(new Func1, Integer>() { @Override public Integer call(Tuple2 result) { - return result.current / result.count; // may throw DivisionByZero, this should be correct... + if (result.count == 0) { + throw new IllegalArgumentException("Sequence contains no elements"); + } + return result.current / result.count; } }); } @@ -58,7 +61,10 @@ public Tuple2 call(Tuple2 accu, Long next) { }).map(new Func1, Long>() { @Override public Long call(Tuple2 result) { - return result.current / result.count; // may throw DivisionByZero, this should be correct... + if (result.count == 0) { + throw new IllegalArgumentException("Sequence contains no elements"); + } + return result.current / result.count; } }); } @@ -73,7 +79,7 @@ public Tuple2 call(Tuple2 accu, Float next) { @Override public Float call(Tuple2 result) { if (result.count == 0) { - throw new ArithmeticException("divide by zero"); + throw new IllegalArgumentException("Sequence contains no elements"); } return result.current / result.count; } @@ -90,7 +96,7 @@ public Tuple2 call(Tuple2 accu, Double next) { @Override public Double call(Tuple2 result) { if (result.count == 0) { - throw new ArithmeticException("divide by zero"); + throw new IllegalArgumentException("Sequence contains no elements"); } return result.current / result.count; } diff --git a/rxjava-core/src/test/java/rx/operators/OperationAverageTest.java b/rxjava-core/src/test/java/rx/operators/OperationAverageTest.java index a8ffa4706b..357743da17 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationAverageTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperationAverageTest.java @@ -52,7 +52,7 @@ public void testEmptyAverage() throws Throwable { average(src).subscribe(w); verify(w, never()).onNext(anyInt()); - verify(w, times(1)).onError(any(ArithmeticException.class)); + verify(w, times(1)).onError(isA(IllegalArgumentException.class)); verify(w, never()).onCompleted(); } @@ -73,7 +73,7 @@ public void testEmptyAverageLongs() throws Throwable { averageLongs(src).subscribe(wl); verify(wl, never()).onNext(anyLong()); - verify(wl, times(1)).onError(any(ArithmeticException.class)); + verify(wl, times(1)).onError(isA(IllegalArgumentException.class)); verify(wl, never()).onCompleted(); } @@ -94,7 +94,7 @@ public void testEmptyAverageFloats() throws Throwable { averageFloats(src).subscribe(wf); verify(wf, never()).onNext(anyFloat()); - verify(wf, times(1)).onError(any(ArithmeticException.class)); + verify(wf, times(1)).onError(isA(IllegalArgumentException.class)); verify(wf, never()).onCompleted(); } @@ -115,7 +115,7 @@ public void testEmptyAverageDoubles() throws Throwable { averageDoubles(src).subscribe(wd); verify(wd, never()).onNext(anyDouble()); - verify(wd, times(1)).onError(any(ArithmeticException.class)); + verify(wd, times(1)).onError(isA(IllegalArgumentException.class)); verify(wd, never()).onCompleted(); } }