TheAlgorithms/Java · error · IllegalArgumentException
Length must be greater than 0
Error message
Length must be greater than 0
What it means
Thrown by Area.surfaceAreaCuboid when the 'length' argument is <= 0. This is the first of three sequential guards (length, then width, then height) on the cuboid surface-area formula 2*(l*w + l*h + w*h); a non-positive length is caught before width/height are even checked.
Source
Thrown at src/main/java/com/thealgorithms/maths/Area.java:48
*/
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");
}
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) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass length > 0 as the first argument: surfaceAreaCuboid(2, 3, 4).
- Validate all three dimensions up front so the error names the real offender.
- Build a small Dimensions value object that rejects non-positive values at construction.
Example fix
// before
double a = Area.surfaceAreaCuboid(l, w, h);
// after
if (l <= 0 || w <= 0 || h <= 0) throw new IllegalArgumentException("dimensions must be > 0");
double a = Area.surfaceAreaCuboid(l, w, h); Defensive patterns
Strategy: validation
Validate before calling
if (length <= 0 || width <= 0 || height <= 0) {
throw new IllegalArgumentException("cuboid dimensions must be > 0");
}
double a = Area.surfaceAreaCuboid(length, width, height); Type guard
static boolean allPositive(double... dims) {
for (double d : dims) if (!(d > 0)) return false;
return true;
} Try / catch
try {
double a = Area.surfaceAreaCuboid(l, w, h);
} catch (IllegalArgumentException e) {
// one of l/w/h was non-positive; identify which
} Prevention
- Validate all three dimensions before the call for a single clear error.
- Keep argument order (length, width, height) consistent across your code.
- Use a Dimensions value object that rejects non-positive values at construction.
When it happens
Trigger: surfaceAreaCuboid(0, 2, 2); surfaceAreaCuboid(-1, 2, 2); any call where the first dimension is non-positive.
Common situations: Passing a partially-populated dimensions object where length defaulted to 0; sign error in a conversion producing a negative length.
Related errors
- Side length must be greater than 0
- Width must be greater than 0
- Height must be greater than 0
- Radius must be greater than 0
- slant height must be greater than 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/6eb46f4956ecf6c6.
Report an issue: GitHub.