TheAlgorithms/Java · error · IllegalArgumentException
Target must be non-negative
Error message
Target must be non-negative
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/recursion/DiceThrower.java:35
*
* @author BEASTSHRIRAM
* @see <a href="https://en.wikipedia.org/wiki/Backtracking">Backtracking Algorithm</a>
*/
public final class DiceThrower {
private DiceThrower() {
// Utility class
}
/**
* Returns all possible dice roll combinations that sum to the target
*
* @param target the target sum to achieve with dice rolls
* @return list of all possible combinations as strings
*/
public static List<String> getDiceCombinations(int target) {
if (target < 0) {
throw new IllegalArgumentException("Target must be non-negative");
}
return generateCombinations("", target);
}
/**
* Prints all possible dice roll combinations that sum to the target
*
* @param target the target sum to achieve with dice rolls
*/
public static void printDiceCombinations(int target) {
if (target < 0) {
throw new IllegalArgumentException("Target must be non-negative");
}
printCombinations("", target);
}
/**
* Recursive helper method to generate all combinationsView on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
List<String> c = DiceThrower.getDiceCombinations(target); // target may be negative
// after
if (target < 0) throw new IllegalArgumentException("target must be >= 0");
List<String> c = DiceThrower.getDiceCombinations(target); Defensive patterns
Strategy: validation
Validate before calling
if (target < 0) {
throw new IllegalArgumentException("target must be >= 0");
}
List<String> c = DiceThrower.getDiceCombinations(target); Type guard
static boolean validDiceTarget(int t) {
return t >= 0;
} Try / catch
try {
List<String> c = DiceThrower.getDiceCombinations(target);
} catch (IllegalArgumentException e) {
logger.warn("Negative dice target: {}", target);
} Prevention
- 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.
When it happens
Trigger: Call getDiceCombinations(-1) or any negative target. Note only negativity is rejected; target 0 is valid (returns [""]).
Common situations: Computing target as a difference that went negative; subtracting a bonus from a small target; parsing user input without range checking.
Related errors
- Number of discs must be non-negative
- number is negative
- n must be a non-negative integer
- Input must be non-negative. Received:
- Input must be a positive integer. Provided:
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d8621476d9b60241.
Report an issue: GitHub.