{"record":{"id":"ff452248579b49d3","repo":"TheAlgorithms/Java","slug":"number-is-negative-ff4522","errorCode":null,"errorMessage":"number is negative","messagePattern":"number is negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/recursion/FactorialRecursion.java","lineNumber":14,"sourceCode":"package com.thealgorithms.recursion;\n\npublic final class FactorialRecursion {\n    private FactorialRecursion() {\n    }\n    /**\n     * Recursive FactorialRecursion Method\n     *\n     * @param n The number to factorial\n     * @return The factorial of the number\n     */\n    public static long factorial(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"number is negative\");\n        }\n        return n == 0 || n == 1 ? 1 : n * factorial(n - 1);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":19,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/recursion/FactorialRecursion.java#L1-L19","documentation":"Thrown by FactorialRecursion.factorial(n) when n < 0. Factorial is defined only for non-negative integers; the recursion n * factorial(n-1) would diverge for negatives. n == 0 and n == 1 are both base cases returning 1. Note: no overflow guard — large n silently overflows long before any recursion-depth issue.","triggerScenarios":"Call factorial(-1) or any negative n. Values like factorial(30) do NOT throw but overflow long; factorial(1000s) may StackOverflowError instead of this exception.","commonSituations":"Subtracting from n in a loop that dipped below zero; parsed user input not range-checked; combinatorial code computing n-k with k > n.","solutions":["Validate n >= 0 at the caller.","For large n, also guard against long overflow (factorial > 20 overflows long) — switch to BigInteger if needed.","Ensure intermediate computations like (n - k) stay non-negative before passing to factorial."],"exampleFix":"// before\nlong f = FactorialRecursion.factorial(n); // n may be negative\n\n// after\nif (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nlong f = FactorialRecursion.factorial(n);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"n must be >= 0\");\n}\nlong f = FactorialRecursion.factorial(n);","typeGuard":"static boolean validFactorialInput(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    long f = FactorialRecursion.factorial(n);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Negative factorial input: {}\", n);\n}","preventionTips":["Range-check n at the input boundary.","Guard n <= 20 to avoid silent long overflow (factorial(21) overflows long).","Ensure intermediate values like (n - k) stay non-negative before passing."],"tags":["recursion","input-validation","illegal-argument","factorial","overflow-risk"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}