{"record":{"id":"677451b2d896b7e1","repo":"TheAlgorithms/Java","slug":"triangle-cannot-be-formed-with-the-given-side-leng","errorCode":null,"errorMessage":"Triangle cannot be formed with the given side lengths (violates triangle inequality)","messagePattern":"Triangle cannot be formed with the given side lengths \\(violates triangle inequality\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/HeronsFormula.java","lineNumber":73,"sourceCode":"     * <p>\n     * Given three side lengths a, b, and c, the area is computed as:\n     * Area = √(s(s - a)(s - b)(s - c))\n     * where s is the semi-perimeter: s = (a + b + c) / 2\n     * </p>\n     *\n     * @param a the length of the first side (must be positive)\n     * @param b the length of the second side (must be positive)\n     * @param c the length of the third side (must be positive)\n     * @return the area of the triangle\n     * @throws IllegalArgumentException if any side length is non-positive or if the\n     *                                  sides cannot form a valid triangle\n     */\n    public static double herons(final double a, final double b, final double c) {\n        if (!areAllSidesPositive(a, b, c)) {\n            throw new IllegalArgumentException(\"All side lengths must be positive\");\n        }\n        if (!canFormTriangle(a, b, c)) {\n            throw new IllegalArgumentException(\"Triangle cannot be formed with the given side lengths (violates triangle inequality)\");\n        }\n        final double s = (a + b + c) / 2.0;\n        return Math.sqrt((s) * (s - a) * (s - b) * (s - c));\n    }\n}\n","sourceCodeStart":55,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/HeronsFormula.java#L55-L79","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndouble area = HeronsFormula.herons(a, b, c);\n\n// after\nboolean validTriangle = a + b > c && b + c > a && c + a > b;\nif (!validTriangle) {\n    throw new IllegalArgumentException(\"Sides violate triangle inequality\");\n}\ndouble area = HeronsFormula.herons(a, b, c);","handlingStrategy":"validation","validationCode":"if (!(a + b > c && b + c > a && c + a > b)) {\n    throw new IllegalArgumentException(\"Sides violate triangle inequality\");\n}\ndouble area = HeronsFormula.herons(a, b, c);","typeGuard":"static boolean canFormTriangle(double a, double b, double c) {\n    return a > 0 && b > 0 && c > 0 && a + b > c && b + c > a && c + a > b;\n}","tryCatchPattern":"try {\n    double area = HeronsFormula.herons(a, b, c);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"triangle inequality\")) {\n        // sides cannot form a valid triangle\n    }\n}","preventionTips":["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."],"tags":["math","geometry","herons-formula","triangle-inequality","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}