{"record":{"id":"ba0da3fe86b343fc","repo":"TheAlgorithms/Java","slug":"index-must-be-non-negative","errorCode":null,"errorMessage":"Index must be non-negative","messagePattern":"Index must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/CatalanNumbers.java","lineNumber":18,"sourceCode":"package com.thealgorithms.maths;\n\n/**\n * Calculate Catalan Numbers\n */\npublic final class CatalanNumbers {\n    private CatalanNumbers() {\n    }\n\n    /**\n     * Calculate the nth Catalan number using a recursive formula.\n     *\n     * @param n the index of the Catalan number to compute\n     * @return the nth Catalan number\n     */\n    public static long catalan(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Index must be non-negative\");\n        }\n        return factorial(2 * n) / (factorial(n + 1) * factorial(n));\n    }\n\n    /**\n     * Calculate the factorial of a number.\n     *\n     * @param n the number to compute the factorial for\n     * @return the factorial of n\n     */\n    private static long factorial(final int n) {\n        if (n == 0 || n == 1) {\n            return 1;\n        }\n        long result = 1;\n        for (int i = 2; i <= n; i++) {\n            result *= i;\n        }","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/CatalanNumbers.java#L1-L36","documentation":"Thrown by CatalanNumbers.catalan(int n) when n is negative. Catalan numbers C(n) are defined for n >= 0; a negative index has no mathematical meaning. Additionally, the computation calls factorial(2*n) which with a negative n would compute factorial of a negative number — the private factorial method would return 1 via its loop (since i starts at 2 > negative n), producing a wrong result rather than crashing. The guard prevents this silent incorrect behavior.","triggerScenarios":"Calling CatalanNumbers.catalan(-1), CatalanNumbers.catalan(-5), or passing a computed index that is negative due to an arithmetic underflow (e.g., catalan(k - 2) where k < 2).","commonSituations":"An off-by-one error in a recurrence or loop. A user-supplied index from unvalidated input (CLI argument, API parameter). A subtraction in the index computation that produces a negative value without a prior range check.","solutions":["Validate n >= 0 before calling CatalanNumbers.catalan(n).","Review loop/recurrence boundaries where n is computed to ensure no underflow.","Add range validation at the input boundary to reject negative indices early."],"exampleFix":"// before\nCatalanNumbers.catalan(-1); // throws 'Index must be non-negative'\n\n// after\nif (n >= 0) {\n    long catalan = CatalanNumbers.catalan(n);\n} else {\n    throw new IllegalArgumentException(\"Catalan index must be >= 0, got: \" + n);\n}","handlingStrategy":"validation","validationCode":"// Validate n before calling CatalanNumbers.catalan\nif (n < 0) {\n    throw new IllegalArgumentException(\"Catalan index must be >= 0, got: \" + n);\n}\nlong catalan = CatalanNumbers.catalan(n);","typeGuard":"static boolean isValidCatalanIndex(int n) {\n    return n >= 0;\n}","tryCatchPattern":null,"preventionTips":["Validate n >= 0 before calling catalan().","Check arithmetic that computes n — especially subtractions that could underflow.","Note: catalan() uses long arithmetic and can overflow for large n (factorial(2*n) grows fast); consider BigInteger for n > ~30."],"tags":["combinatorics","input-validation","catalan-numbers","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}