TheAlgorithms/Java · error · IllegalArgumentException

Radius must be greater than 0

Error message

Radius must be greater than 0

What it means

Thrown by Area.surfaceAreaSphere when radius <= 0. The message comes from the POSITIVE_RADIUS constant ('Radius must be greater than 0'). The sphere surface area 4*pi*r^2 requires a positive radius, so zero/negative radii are rejected.

Source

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

        }
        if (width <= 0) {
            throw new IllegalArgumentException("Width must be greater than 0");
        }
        if (height <= 0) {
            throw new IllegalArgumentException("Height must be greater than 0");
        }
        return 2 * (length * width + length * height + width * height);
    }

    /**
     * Calculate the surface area of a sphere.
     *
     * @param radius radius of sphere
     * @return surface area of given sphere
     */
    public static double surfaceAreaSphere(final double radius) {
        if (radius <= 0) {
            throw new IllegalArgumentException(POSITIVE_RADIUS);
        }
        return 4 * Math.PI * radius * radius;
    }

    /**
     * Calculate the surface area of a pyramid with a square base.
     *
     * @param sideLength side length of the square base
     * @param slantHeight slant height of the pyramid
     * @return surface area of the given pyramid
     */
    public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
        if (sideLength <= 0) {
            throw new IllegalArgumentException("");
        }
        if (slantHeight <= 0) {
            throw new IllegalArgumentException("slant height must be greater than 0");
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass radius > 0: surfaceAreaSphere(3.0).
  2. Guard the radius before calling and report a domain error.
  3. Where radius is a distance, reject the degenerate same-point case upstream.

Example fix

// before
double a = Area.surfaceAreaSphere(r);
// after
if (r <= 0) throw new IllegalArgumentException("radius must be > 0");
double a = Area.surfaceAreaSphere(r);
Defensive patterns

Strategy: validation

Validate before calling

if (!(radius > 0)) {
    throw new IllegalArgumentException("sphere radius must be > 0");
}
double a = Area.surfaceAreaSphere(radius);

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: surfaceAreaSphere(0); surfaceAreaSphere(-5.0); passing a radius computed from a distance that came out 0.

Common situations: Radius derived as a difference of identical points (0); default 0.0 field; negative radius from a signed-distance calculation.

Related errors


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