{"record":{"id":"7b95d3ce4df03ac3","repo":"TheAlgorithms/Java","slug":"all-side-lengths-must-be-positive","errorCode":null,"errorMessage":"All side lengths must be positive","messagePattern":"All side lengths must be positive","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/HeronsFormula.java","lineNumber":70,"sourceCode":"\n    /**\n     * Calculates the area of a triangle using Heron's Formula.\n     * <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":52,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/HeronsFormula.java#L52-L79","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndouble area = HeronsFormula.herons(a, b, c);\n\n// after\nif (a <= 0 || b <= 0 || c <= 0) {\n    throw new IllegalArgumentException(\"All sides must be positive: a=\" + a + \", b=\" + b + \", c=\" + c);\n}\ndouble area = HeronsFormula.herons(a, b, c);","handlingStrategy":"validation","validationCode":"if (a <= 0 || b <= 0 || c <= 0) {\n    throw new IllegalArgumentException(\"All sides must be positive\");\n}\ndouble area = HeronsFormula.herons(a, b, c);","typeGuard":"static boolean allSidesPositive(double a, double b, double c) {\n    return a > 0 && b > 0 && c > 0;\n}","tryCatchPattern":"try {\n    double area = HeronsFormula.herons(a, b, c);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"positive\")) {\n        // non-positive side; handle invalid geometry\n    }\n}","preventionTips":["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."],"tags":["math","geometry","herons-formula","argument-validation","triangle"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}