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 coefficientsView on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a positive integer order: `new EMAFilter` analog `new IIRFilter(n)` with n >= 1.
- Validate the order before construction and reject/correct non-positive values at the source.
- 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
- Validate order against the design specification before constructing.
- If order is computed from cutoff/ Fs, clamp the result to at least 1.
- Treat a missing/unset order as an error, not as 0.
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
- Alpha must be between 0 and 1.
- aCoeffs must be of size {}, got {}
- aCoeffs.get(0) must not be zero
- bCoeffs must be of size {}, got {}
- Invalid input: 0 ≤ k ≤ n is required.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8d002d67c2c75c1a.
Report an issue: GitHub.