{"record":{"id":"73dc18b71079b448","repo":"TheAlgorithms/Java","slug":"n-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"n must be a non-negative integer","messagePattern":"n must be a non-negative integer","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/recursion/FibonacciSeries.java","lineNumber":36,"sourceCode":" * <li>{@link com.thealgorithms.maths.FibonacciNumberCheck} - Utility to check if a given number is a Fibonacci number</li>\n * <li>{@link com.thealgorithms.matrix.matrixexponentiation.Fibonacci} - O(log n) Matrix Exponentiation approach</li>\n * </ul>\n */\npublic final class FibonacciSeries {\n    private FibonacciSeries() {\n        throw new UnsupportedOperationException(\"Utility class\");\n    }\n\n    /**\n     * Calculates the nth term in the Fibonacci sequence using recursion.\n     *\n     * @param n the position in the Fibonacci sequence (must be non-negative)\n     * @return the nth Fibonacci number\n     * @throws IllegalArgumentException if n is negative\n     */\n    public static int fibonacci(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"n must be a non-negative integer\");\n        }\n        if (n <= 1) {\n            return n;\n        }\n        return fibonacci(n - 1) + fibonacci(n - 2);\n    }\n}\n","sourceCodeStart":18,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java#L18-L44","documentation":"Thrown by FibonacciSeries.fibonacci(n) when n < 0. The Fibonacci sequence is indexed from 0, and the naive recursion fibonacci(n-1)+fibonacci(n-2) has no base case for negatives, so the guard rejects them. n <= 1 returns n (base cases). Note: exponential time O(2^n) — large n is slow, not erroneous.","triggerScenarios":"Call fibonacci(-1) or any negative n. Very large positive n (e.g. 50+) does not throw but is extremely slow and will overflow int around n=47.","commonSituations":"Computing n from a decremented counter that went negative; parsed index not validated; off-by-one in a loop bound feeding fibonacci(i-1) when i could be 0.","solutions":["Validate n >= 0 at the caller.","For repeated/large queries, memoize or use an iterative/matrix approach instead of naive recursion.","Guard n <= 46 if using int return type to avoid silent overflow (fib(47) overflows int)."],"exampleFix":"// before\nint f = FibonacciSeries.fibonacci(n); // n may be negative\n\n// after\nif (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nint f = FibonacciSeries.fibonacci(n);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"n must be >= 0\");\n}\nint f = FibonacciSeries.fibonacci(n);","typeGuard":"static boolean validFibIndex(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    int f = FibonacciSeries.fibonacci(n);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Negative Fibonacci index: {}\", n);\n}","preventionTips":["Range-check n at the input boundary.","Guard n <= 46 to avoid silent int overflow (fib(47) overflows int).","For repeated or large queries, replace naive recursion with memoization or an iterative method."],"tags":["recursion","input-validation","illegal-argument","fibonacci","overflow-risk"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}