{"record":{"id":"8d002d67c2c75c1a","repo":"TheAlgorithms/Java","slug":"order-must-be-greater-than-zero","errorCode":null,"errorMessage":"order must be greater than zero","messagePattern":"order must be greater than zero","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/audiofilters/IIRFilter.java","lineNumber":25,"sourceCode":" * <a href=\"https://en.wikipedia.org/wiki/Infinite_impulse_response\">Wikipedia link</a>\n */\npublic class IIRFilter {\n\n    private final int order;\n    private final double[] coeffsA;\n    private final double[] coeffsB;\n    private final double[] historyX;\n    private final double[] historyY;\n\n    /**\n     * Construct an IIR Filter\n     *\n     * @param order the filter's order\n     * @throws IllegalArgumentException if order is zero or less\n     */\n    public IIRFilter(int order) throws IllegalArgumentException {\n        if (order < 1) {\n            throw new IllegalArgumentException(\"order must be greater than zero\");\n        }\n\n        this.order = order;\n        coeffsA = new double[order + 1];\n        coeffsB = new double[order + 1];\n\n        // Sane defaults\n        coeffsA[0] = 1.0;\n        coeffsB[0] = 1.0;\n\n        historyX = new double[order];\n        historyY = new double[order];\n    }\n\n    /**\n     * Set coefficients\n     *\n     * @param aCoeffs Denominator coefficients","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java#L7-L43","documentation":"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.","triggerScenarios":"Constructing `new IIRFilter(0)` or `new IIRFilter(-2)`. The guard `order < 1` rejects zero and any negative value; any order >= 1 is accepted.","commonSituations":"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.","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."],"exampleFix":"// before\nnew IIRFilter(0);   // throws\n\n// after\nint order = Math.max(1, computedOrder);\nnew IIRFilter(order);","handlingStrategy":"validation","validationCode":"if (order < 1) throw new IllegalArgumentException(\"order must be >= 1\");\nIIRFilter filter = new IIRFilter(order);","typeGuard":"public static boolean isValidOrder(int order) {\n    return order >= 1;\n}","tryCatchPattern":"try {\n    filter = new IIRFilter(order);\n} catch (IllegalArgumentException e) {\n    log.warn(\"Invalid filter order {}, defaulting to 1\", order);\n    filter = new IIRFilter(1);\n}","preventionTips":["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."],"tags":["audio","filter","constructor","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}