TheAlgorithms/Java · error · IllegalArgumentException
Width must be greater than 0
Error message
Width must be greater than 0
What it means
Thrown by Area.surfaceAreaCuboid when the 'width' argument is <= 0. It fires only after length passes its guard (length > 0) but before height is checked, so it specifically indicates a non-positive width with a valid length.
Source
Thrown at src/main/java/com/thealgorithms/maths/Area.java:51
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) {
throw new IllegalArgumentException(POSITIVE_RADIUS);
}
return 4 * Math.PI * radius * radius;View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass width > 0 as the second argument: surfaceAreaCuboid(2, 3, 4).
- Validate the full dimension set before the call to get one consolidated error.
- Confirm the argument order matches the signature (length, width, height).
Example fix
// before
double a = Area.surfaceAreaCuboid(l, w, h);
// after
if (w <= 0) throw new IllegalArgumentException("width must be > 0");
double a = Area.surfaceAreaCuboid(l, w, h); Defensive patterns
Strategy: validation
Validate before calling
if (width <= 0) {
throw new IllegalArgumentException("cuboid width must be > 0");
}
double a = Area.surfaceAreaCuboid(length, width, height); Type guard
static boolean isPositiveDimension(double d) {
return d > 0 && Double.isFinite(d);
} Try / catch
try {
double a = Area.surfaceAreaCuboid(l, w, h);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("Width")) { /* width offender */ }
} Prevention
- Confirm argument order: width is the second parameter.
- Pre-validate the full set so the right field is named.
- Centralize dimension validation to avoid per-method duplication.
When it happens
Trigger: surfaceAreaCuboid(2, 0, 2); surfaceAreaCuboid(2, -3, 4); a valid length paired with a zero/negative width.
Common situations: Dimensions loaded from different sources where width came in as 0 or negative; column-vs-row index mix-up producing a 0 width.
Related errors
- Side length must be greater than 0
- Length 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/1b6b472df214a9d8.
Report an issue: GitHub.