TheAlgorithms/Java · error · IllegalArgumentException

order must be greater than zero

Error message

order must be greater than zero

What it means

Thrown by the IIRFilter constructor when the requested filter order is less than 1. The order determines the size of the coefficient and history arrays, so a non-positive order would leave the filter with no memory and make processing meaningless. The filter needs at least one tap to function as a recursive filter.

Source

Thrown at src/main/java/com/thealgorithms/audiofilters/IIRFilter.java:25

 * <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">Wikipedia link</a>
 */
public class IIRFilter {

    private final int order;
    private final double[] coeffsA;
    private final double[] coeffsB;
    private final double[] historyX;
    private final double[] historyY;

    /**
     * Construct an IIR Filter
     *
     * @param order the filter's order
     * @throws IllegalArgumentException if order is zero or less
     */
    public IIRFilter(int order) throws IllegalArgumentException {
        if (order < 1) {
            throw new IllegalArgumentException("order must be greater than zero");
        }

        this.order = order;
        coeffsA = new double[order + 1];
        coeffsB = new double[order + 1];

        // Sane defaults
        coeffsA[0] = 1.0;
        coeffsB[0] = 1.0;

        historyX = new double[order];
        historyY = new double[order];
    }

    /**
     * Set coefficients
     *
     * @param aCoeffs Denominator coefficients

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a positive integer order: `new EMAFilter` analog `new IIRFilter(n)` with n >= 1.
  2. Validate the order before construction and reject/correct non-positive values at the source.
  3. If order is computed from filter parameters, ensure the design formula clamps the result to at least 1.

Example fix

// before
new IIRFilter(0);   // throws

// after
int order = Math.max(1, computedOrder);
new IIRFilter(order);
Defensive patterns

Strategy: validation

Validate before calling

if (order < 1) throw new IllegalArgumentException("order must be >= 1");
IIRFilter filter = new IIRFilter(order);

Type guard

public static boolean isValidOrder(int order) {
    return order >= 1;
}

Try / catch

try {
    filter = new IIRFilter(order);
} catch (IllegalArgumentException e) {
    log.warn("Invalid filter order {}, defaulting to 1", order);
    filter = new IIRFilter(1);
}

Prevention

When it happens

Trigger: Constructing `new IIRFilter(0)` or `new IIRFilter(-2)`. The guard `order < 1` rejects zero and any negative value; any order >= 1 is accepted.

Common situations: Order read from a configuration file that defaults to 0 when unset; computing order from a cutoff frequency that yields 0 due to rounding; passing a parsed integer that failed to parse and fell back to a default of 0.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/8d002d67c2c75c1a. Report an issue: GitHub.