TheAlgorithms/Java · error · IllegalArgumentException
All side lengths must be positive
Error message
All side lengths must be positive
What it means
Thrown by HeronsFormula.herons(double a, double b, double c) when any of the three side lengths is non-positive (a <= 0, b <= 0, or c <= 0). The helper areAllSidesPositive requires a > 0 AND b > 0 AND c > 0. Side lengths must be positive for the geometric formula to be meaningful, as zero or negative lengths are not valid triangle sides.
Source
Thrown at src/main/java/com/thealgorithms/maths/HeronsFormula.java:70
/**
* Calculates the area of a triangle using Heron's Formula.
* <p>
* Given three side lengths a, b, and c, the area is computed as:
* Area = √(s(s - a)(s - b)(s - c))
* where s is the semi-perimeter: s = (a + b + c) / 2
* </p>
*
* @param a the length of the first side (must be positive)
* @param b the length of the second side (must be positive)
* @param c the length of the third side (must be positive)
* @return the area of the triangle
* @throws IllegalArgumentException if any side length is non-positive or if the
* sides cannot form a valid triangle
*/
public static double herons(final double a, final double b, final double c) {
if (!areAllSidesPositive(a, b, c)) {
throw new IllegalArgumentException("All side lengths must be positive");
}
if (!canFormTriangle(a, b, c)) {
throw new IllegalArgumentException("Triangle cannot be formed with the given side lengths (violates triangle inequality)");
}
final double s = (a + b + c) / 2.0;
return Math.sqrt((s) * (s - a) * (s - b) * (s - c));
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate all three sides are strictly positive before calling herons().
- Guard against zero or negative values from measurement data with a threshold check (e.g., side > 1e-10).
- Return a sentinel or Optional for degenerate geometry cases instead of passing invalid values.
Example fix
// before
double area = HeronsFormula.herons(a, b, c);
// after
if (a <= 0 || b <= 0 || c <= 0) {
throw new IllegalArgumentException("All sides must be positive: a=" + a + ", b=" + b + ", c=" + c);
}
double area = HeronsFormula.herons(a, b, c); Defensive patterns
Strategy: validation
Validate before calling
if (a <= 0 || b <= 0 || c <= 0) {
throw new IllegalArgumentException("All sides must be positive");
}
double area = HeronsFormula.herons(a, b, c); Type guard
static boolean allSidesPositive(double a, double b, double c) {
return a > 0 && b > 0 && c > 0;
} Try / catch
try {
double area = HeronsFormula.herons(a, b, c);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("positive")) {
// non-positive side; handle invalid geometry
}
} Prevention
- Validate all three sides are strictly positive before calling.
- Use a small epsilon threshold for floating-point measurement data.
- Reject or sanitize zero-valued fields from default-initialized objects.
When it happens
Trigger: Calling herons(0, 4, 5), herons(-3, 4, 5), herons(3, 0, 0), or any combination where at least one side is <= 0. This fires before the triangle-inequality check (error 416), so negative sides always hit this message first.
Common situations: Geometry computations with degenerate or uninitialized inputs. Data from sensors or measurements that return zero on failure. Default-constructed objects with zero-valued fields passed to the formula.
Related errors
- Triangle cannot be formed with the given side lengths (viola
- Input 'n' is too big to give accurate result.
- k must be between 1 and the size of the array
- Array must be non-empty.
- Array must be non-empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7b95d3ce4df03ac3.
Report an issue: GitHub.