{"record":{"id":"ca56d8f949defa9b","repo":"TheAlgorithms/Java","slug":"number-must-be-positive","errorCode":null,"errorMessage":"Number must be positive.","messagePattern":"Number must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/AbundantNumber.java","lineNumber":31,"sourceCode":"\n    private AbundantNumber() {\n    }\n\n    // Function to calculate sum of all divisors including n\n    private static int sumOfDivisors(int n) {\n        int sum = 1 + n; // 1 and n are always divisors\n        for (int i = 2; i <= n / 2; i++) {\n            if (n % i == 0) {\n                sum += i; // adding divisor to sum\n            }\n        }\n        return sum;\n    }\n\n    // Common validation method\n    private static void validatePositiveNumber(int number) {\n        if (number <= 0) {\n            throw new IllegalArgumentException(\"Number must be positive.\");\n        }\n    }\n\n    /**\n     * Check if {@code number} is an Abundant number or not by checking sum of divisors > 2n\n     *\n     * @param number the number\n     * @return {@code true} if {@code number} is an Abundant number, otherwise false\n     */\n    public static boolean isAbundant(int number) {\n        validatePositiveNumber(number);\n\n        return sumOfDivisors(number) > 2 * number;\n    }\n\n    /**\n     * Check if {@code number} is an Abundant number or not by checking Aliquot Sum > n\n     *","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/AbundantNumber.java#L13-L49","documentation":"Thrown by AbundantNumber.validatePositiveNumber when 'number' is <= 0. Abundant-number logic is defined only over positive integers (the divisor sum from 1..n/2 is meaningless for 0 or negatives), so isAbundant rejects anything non-positive up front via this shared validator.","triggerScenarios":"isAbundant(0); isAbundant(-5); calling isAbundant with a value parsed from empty/invalid user input that defaulted to 0; calling the internal sumOfDivisors path with a zero default.","commonSituations":"Parsing 'how many' inputs where 0 is a natural sentinel; off-by-one loops that reach 0; tests using 0 as a boundary value; negative inputs from subtraction that underflows past the domain.","solutions":["Pass a positive integer: isAbundant(12).","Validate before calling: if (n > 0) isAbundant(n) else report invalid.","Treat <= 0 as 'not applicable' and branch around the call instead of forwarding it."],"exampleFix":"// before\nboolean ab = AbundantNumber.isAbundant(count);\n// after\nif (count <= 0) throw new IllegalArgumentException(\"count must be > 0\");\nboolean ab = AbundantNumber.isAbundant(count);","handlingStrategy":"validation","validationCode":"if (n <= 0) {\n    throw new IllegalArgumentException(\"n must be a positive integer\");\n}\nboolean abundant = AbundantNumber.isAbundant(n);","typeGuard":"static boolean isPositiveInt(int n) {\n    return n > 0;\n}","tryCatchPattern":"try {\n    boolean ab = AbundantNumber.isAbundant(n);\n} catch (IllegalArgumentException e) {\n    // n was <= 0; report invalid input\n}","preventionTips":["Reject 0/negative at the input boundary (parser/UI), not at the math call.","Use 1, not 0, as the start value for search loops feeding isAbundant.","Add a unit test for the 0 and negative boundaries."],"tags":["validation","maths","precondition","positive-number","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}