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.surfaceAreaCube when sideLength <= 0. A cube must have a positive side length for the 6*s^2 formula to represent a real geometric surface area; zero or negative lengths are physically meaningless and rejected at entry.

Source

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

    /**
     * String of IllegalArgumentException for height
     */
    private static final String POSITIVE_HEIGHT = "Height must be greater than 0";

    /**
     * String of IllegalArgumentException for base
     */
    private static final String POSITIVE_BASE = "Base must be greater than 0";

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

    /**
     * Calculate the surface area of a cuboid.
     *
     * @param length length of the cuboid
     * @param width width of the cuboid
     * @param height height of the cuboid
     * @return surface area of given cuboid
     */
    public static double surfaceAreaCuboid(final double length, double width, double height) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be greater than 0");
        }
        if (width <= 0) {
            throw new IllegalArgumentException("Width must be greater than 0");

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a positive side length: surfaceAreaCube(3.0).
  2. Validate the dimension before calling and surface a domain error to the user.
  3. Ensure measurement inputs are loaded/defaulted to a positive value, not 0.

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try {
    double a = Area.surfaceAreaCube(side);
} catch (IllegalArgumentException e) {
    // non-positive side; prompt user for valid measurement
}

Prevention

When it happens

Trigger: surfaceAreaCube(0); surfaceAreaCube(-2.0); passing an uninitialized/zeroed measurement field.

Common situations: Default double field of 0.0 used before measurement assignment; user form left blank parsing to 0; unit conversion error producing a tiny/negative value.

Related errors


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