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
- Pass a positive side length: surfaceAreaCube(3.0).
- Validate the dimension before calling and surface a domain error to the user.
- 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
- Default measurement fields to a sentinel and validate, not to 0.0.
- Check for NaN/Infinity too, since doubles can carry them.
- Centralize dimension validation in one helper for all Area calls.
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
- 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/3508b2418f9a9ad9.
Report an issue: GitHub.