TheAlgorithms/Java · error · IllegalArgumentException
Triangle cannot be formed with the given side lengths (viola
Error message
Triangle cannot be formed with the given side lengths (violates triangle inequality)
What it means
Thrown by HeronsFormula.herons(double a, double b, double c) when the sides are all positive but fail the triangle inequality: the method canFormTriangle requires a+b > c AND b+c > a AND c+a > b (all strictly greater). Three positive lengths that cannot form a valid triangle produce this error. It fires only after the positivity check (error 415) passes.
Source
Thrown at src/main/java/com/thealgorithms/maths/HeronsFormula.java:73
* <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
- Verify the triangle inequality before calling herons(): all of a+b>c, b+c>a, c+a>b.
- For near-degenerate cases, add a tolerance: a + b > c + epsilon.
- Validate side lengths from user input or computed geometry against the inequality at the source.
Example fix
// before
double area = HeronsFormula.herons(a, b, c);
// after
boolean validTriangle = a + b > c && b + c > a && c + a > b;
if (!validTriangle) {
throw new IllegalArgumentException("Sides violate triangle inequality");
}
double area = HeronsFormula.herons(a, b, c); Defensive patterns
Strategy: validation
Validate before calling
if (!(a + b > c && b + c > a && c + a > b)) {
throw new IllegalArgumentException("Sides violate triangle inequality");
}
double area = HeronsFormula.herons(a, b, c); Type guard
static boolean canFormTriangle(double a, double b, double c) {
return a > 0 && b > 0 && c > 0 && a + b > c && b + c > a && c + a > b;
} Try / catch
try {
double area = HeronsFormula.herons(a, b, c);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("triangle inequality")) {
// sides cannot form a valid triangle
}
} Prevention
- Check the triangle inequality (strict >) before calling herons().
- Add floating-point tolerance for near-degenerate triangles.
- Validate computed or user-supplied side lengths against the inequality at the source.
When it happens
Trigger: Calling herons(1, 2, 10) (1+2 < 10), herons(1, 1, 3) (1+1 < 3), herons(5, 1, 1), or any sides where the longest side is >= the sum of the other two. Note: the check uses strict >, so degenerate triangles like (2, 3, 5) where 2+3 == 5 also fail.
Common situations: User-provided or computed triangle vertices that yield impossible side lengths. Floating-point precision issues where near-degenerate triangles (e.g., 2.0, 3.0, 5.0000000001) fail the strict inequality. Data errors in geometric or CAD applications.
Related errors
- All side lengths must be positive
- 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/677451b2d896b7e1.
Report an issue: GitHub.