apache/flink · error · IllegalArgumentException

divisor must be != 0

Error message

divisor must be != 0

What it means

MemorySize.divide(long by) throws IllegalArgumentException("divisor must be != 0") when the divisor is negative (the message text is imprecise — the guard actually tests by < 0, so only negative divisors reach it). A zero divisor bypasses this guard and instead throws the JVM's native ArithmeticException("/ by zero").

Source

Thrown at flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java:222

        return new MemorySize(Math.subtractExact(this.bytes, that.bytes));
    }

    public MemorySize multiply(double multiplier) {
        if (multiplier < 0) {
            throw new IllegalArgumentException("multiplier must be >= 0");
        }

        BigDecimal product =
                BigDecimal.valueOf(this.bytes).multiply(BigDecimal.valueOf(multiplier));
        if (product.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
            throw new ArithmeticException("long overflow");
        }
        return new MemorySize(product.longValue());
    }

    public MemorySize divide(long by) {
        if (by < 0) {
            throw new IllegalArgumentException("divisor must be != 0");
        }
        return new MemorySize(bytes / by);
    }

    // ------------------------------------------------------------------------
    //  Parsing
    // ------------------------------------------------------------------------

    /**
     * Parses the given string as as MemorySize.
     *
     * @param text The string to parse
     * @return The parsed MemorySize
     * @throws IllegalArgumentException Thrown, if the expression cannot be parsed.
     */
    public static MemorySize parse(String text) throws IllegalArgumentException {
        return new MemorySize(parseBytes(text));
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Validate the divisor is a positive number before dividing
  2. Fix the upstream config so slot/parallelism counts are >= 1
  3. Note the message says '!= 0' but only negative values hit it; zero gives a separate '/ by zero' ArithmeticException — guard both

Example fix

// before
MemorySize perSlot = total.divide(slots); // slots from config, may be <= 0

// after
if (slots <= 0) {
    throw new IllegalArgumentException("slots must be positive, got " + slots);
}
MemorySize perSlot = total.divide(slots);
Defensive patterns

Strategy: validation

Validate before calling

if (by <= 0) {
    throw new IllegalArgumentException("divisor must be positive, got " + by);
}
MemorySize share = total.divide(by);

Try / catch

catch (IllegalArgumentException | ArithmeticException e) { /* covers negative divisor and '/ by zero'; rethrow with the config key it came from */ }

Prevention

When it happens

Trigger: size.divide(-4); splitting a memory budget by a slot count or parallelism that is configured negative; sign errors in derived divisor values.

Common situations: Dividing a total memory size by task slots, parallelism, or worker count where that value is read from config and can be negative or zero on misconfiguration.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/bf822ad146718243. Report an issue: GitHub.