TheAlgorithms/Java · error · IllegalArgumentException

Side Length must be greater than 0

Error message

Side Length must be greater than 0

What it means

Thrown by Area.surfaceAreaSquare when sideLength <= 0. Note the message capitalization differs from the cube guard: 'Side Length must be greater than 0' (two words, Title Case) vs the cube's 'Side length must be greater than 0'. Tests matching on exact text must account for this.

Source

Thrown at src/main/java/com/thealgorithms/maths/Area.java:133

    public static double surfaceAreaCylinder(final double radius, final double height) {
        if (radius <= 0) {
            throw new IllegalArgumentException(POSITIVE_RADIUS);
        }
        if (height <= 0) {
            throw new IllegalArgumentException(POSITIVE_HEIGHT);
        }
        return 2 * (Math.PI * radius * radius + Math.PI * radius * height);
    }

    /**
     * Calculate the area of a square.
     *
     * @param sideLength side length of square
     * @return area of given square
     */
    public static double surfaceAreaSquare(final double sideLength) {
        if (sideLength <= 0) {
            throw new IllegalArgumentException("Side Length must be greater than 0");
        }
        return sideLength * sideLength;
    }

    /**
     * Calculate the area of a triangle.
     *
     * @param base   base of triangle
     * @param height height of triangle
     * @return area of given triangle
     */
    public static double surfaceAreaTriangle(final double baseLength, final double height) {
        if (baseLength <= 0) {
            throw new IllegalArgumentException(POSITIVE_BASE);
        }
        if (height <= 0) {
            throw new IllegalArgumentException(POSITIVE_HEIGHT);
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass sideLength > 0: surfaceAreaSquare(5).
  2. Validate before calling and report a domain error.
  3. If asserting on the message, use the exact 'Side Length' wording or match on type only.

Example fix

// before
double a = Area.surfaceAreaSquare(side);
// after
if (side <= 0) throw new IllegalArgumentException("side length must be > 0");
double a = Area.surfaceAreaSquare(side);
Defensive patterns

Strategy: validation

Validate before calling

if (!(sideLength > 0)) {
    throw new IllegalArgumentException("square side length must be > 0");
}
double a = Area.surfaceAreaSquare(sideLength);

Type guard

static boolean isPositiveDimension(double d) {
    return d > 0 && Double.isFinite(d);
}

Try / catch

try {
    double a = Area.surfaceAreaSquare(side);
} catch (IllegalArgumentException e) {
    // side non-positive; reject input
}

Prevention

When it happens

Trigger: surfaceAreaSquare(0); surfaceAreaSquare(-4); side defaulted to 0.

Common situations: Default 0.0 side field; negative value from conversion; tests that assume the same message string as the cube method.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/bc9d139aee28a8f3. Report an issue: GitHub.