ReactiveX/RxJava · error · IllegalArgumentException

Integer overflow

Error message

Integer overflow

What it means

Thrown by Observable.range when the inclusive endpoint start + (count - 1) exceeds Integer.MAX_VALUE. Because the operator emits ints, the range must fit in the int domain; the library casts to long to detect overflow safely and fails fast rather than wrapping.

Source

Thrown at src/main/java/io/reactivex/rxjava4/core/Observable.java:2836

     * @see <a href="http://reactivex.io/documentation/operators/range.html">ReactiveX operators documentation: Range</a>
     * @see #rangeLong(long, long)
     * @see #intervalRange(long, long, long, long, TimeUnit)
     */
    @CheckReturnValue
    @SchedulerSupport(SchedulerSupport.NONE)
    @NonNull
    public static Observable<Integer> range(int start, int count) {
        if (count < 0) {
            throw new IllegalArgumentException("count >= 0 required but it was " + count);
        }
        if (count == 0) {
            return empty();
        }
        if (count == 1) {
            return just(start);
        }
        if ((long)start + (count - 1) > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("Integer overflow");
        }
        return RxJavaPlugins.onAssembly(new ObservableRange(start, count));
    }

    /**
     * Returns an {@code Observable} that emits a sequence of {@link Long}s within a specified range.
     * <p>
     * <img width="640" height="195" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/rangeLong.png" alt="">
     * <dl>
     *  <dt><b>Scheduler:</b></dt>
     *  <dd>{@code rangeLong} does not operate by default on a particular {@link Scheduler}.</dd>
     * </dl>
     *
     * @param start
     *            the value of the first {@code Long} in the sequence
     * @param count
     *            the number of sequential {@code Long}s to generate
     * @return the new {@code Observable} instance

View on GitHub (pinned to a8ab535614)

Solutions

  1. Reduce start and/or count so the inclusive end stays <= Integer.MAX_VALUE.
  2. Validate up front: boolean overflow = (long)start + (count - 1) > Integer.MAX_VALUE.
  3. Switch to Observable.rangeLong if you need the long domain.

Example fix

// before
Observable.range(Integer.MAX_VALUE - 10, 100);

// after
// option A: clamp into the int domain
int start = Integer.MAX_VALUE - 10;
int count = Math.min(100, Integer.MAX_VALUE - start + 1);
Observable.range(start, count);
// option B: move to the long domain
Observable.rangeLong((long)Integer.MAX_VALUE - 10, 100L);
Defensive patterns

Strategy: validation

Validate before calling

if ((long)start + (count - 1) > Integer.MAX_VALUE) {
    // reduce count or switch to rangeLong
    count = Integer.MAX_VALUE - start + 1;
}

Try / catch

try {
    return Observable.range(start, count);
} catch (IllegalArgumentException e) {
    // fall back to long-domain range
    return Observable.rangeLong((long)start, (long)count);
}

Prevention

When it happens

Trigger: Calling Observable.range(start, count) where (long)start + (count - 1) > Integer.MAX_VALUE. Happens with high positive start values combined with a large count.

Common situations: Generating ranges that cross the top of the int domain; paging counters near Integer.MAX_VALUE; unvalidated computed ranges.

Related errors


AI-assisted analysis of ReactiveX/RxJava@a8ab535614 (2026-08-13). Data as JSON: /api/errors/04f17c23910a42fa. Report an issue: GitHub.