{"record":{"id":"d8621476d9b60241","repo":"TheAlgorithms/Java","slug":"target-must-be-non-negative","errorCode":null,"errorMessage":"Target must be non-negative","messagePattern":"Target must be non-negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/recursion/DiceThrower.java","lineNumber":35,"sourceCode":" *\n * @author BEASTSHRIRAM\n * @see <a href=\"https://en.wikipedia.org/wiki/Backtracking\">Backtracking Algorithm</a>\n */\npublic final class DiceThrower {\n\n    private DiceThrower() {\n        // Utility class\n    }\n\n    /**\n     * Returns all possible dice roll combinations that sum to the target\n     *\n     * @param target the target sum to achieve with dice rolls\n     * @return list of all possible combinations as strings\n     */\n    public static List<String> getDiceCombinations(int target) {\n        if (target < 0) {\n            throw new IllegalArgumentException(\"Target must be non-negative\");\n        }\n        return generateCombinations(\"\", target);\n    }\n\n    /**\n     * Prints all possible dice roll combinations that sum to the target\n     *\n     * @param target the target sum to achieve with dice rolls\n     */\n    public static void printDiceCombinations(int target) {\n        if (target < 0) {\n            throw new IllegalArgumentException(\"Target must be non-negative\");\n        }\n        printCombinations(\"\", target);\n    }\n\n    /**\n     * Recursive helper method to generate all combinations","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/recursion/DiceThrower.java#L17-L53","documentation":"Thrown by DiceThrower.getDiceCombinations(target) when target < 0. The recursion subtracts dice faces (1-6) from the remaining sum toward a base case of 0, so a negative target has no valid combination of positive faces. target == 0 returns a single empty combination.","triggerScenarios":"Call getDiceCombinations(-1) or any negative target. Note only negativity is rejected; target 0 is valid (returns [\"\"]).","commonSituations":"Computing target as a difference that went negative; subtracting a bonus from a small target; parsing user input without range checking.","solutions":["Validate target >= 0 at the caller and reject early.","If target can legitimately be 0 or small, let it through (0 returns an empty-string combination).","Guard against very large targets too — this enumerates ALL compositions of target with parts 1-6, which grows exponentially and can hang/OOM."],"exampleFix":"// before\nList<String> c = DiceThrower.getDiceCombinations(target); // target may be negative\n\n// after\nif (target < 0) throw new IllegalArgumentException(\"target must be >= 0\");\nList<String> c = DiceThrower.getDiceCombinations(target);","handlingStrategy":"validation","validationCode":"if (target < 0) {\n    throw new IllegalArgumentException(\"target must be >= 0\");\n}\nList<String> c = DiceThrower.getDiceCombinations(target);","typeGuard":"static boolean validDiceTarget(int t) {\n    return t >= 0;\n}","tryCatchPattern":"try {\n    List<String> c = DiceThrower.getDiceCombinations(target);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Negative dice target: {}\", target);\n}","preventionTips":["Range-check target at the input boundary.","Beware large positive targets — the result set grows exponentially and can OOM.","Prefer getDiceCombinations over printDiceCombinations for programmatic use."],"tags":["recursion","input-validation","illegal-argument","backtracking"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}