TheAlgorithms/Java · error · IllegalArgumentException
Base must be greater than 0
Error message
Base must be greater than 0
What it means
Thrown by Area.surfaceAreaTriangle when baseLength <= 0. Uses the POSITIVE_BASE constant ('Base must be greater than 0'). Checked before height, so a non-positive base is reported first.
Source
Thrown at src/main/java/com/thealgorithms/maths/Area.java:147
* @return area of given square
*/
public static double surfaceAreaSquare(final double sideLength) {
if (sideLength <= 0) {
throw new IllegalArgumentException("Side Length must be greater than 0");
}
return sideLength * sideLength;
}
/**
* Calculate the area of a triangle.
*
* @param base base of triangle
* @param height height of triangle
* @return area of given triangle
*/
public static double surfaceAreaTriangle(final double baseLength, final double height) {
if (baseLength <= 0) {
throw new IllegalArgumentException(POSITIVE_BASE);
}
if (height <= 0) {
throw new IllegalArgumentException(POSITIVE_HEIGHT);
}
return baseLength * height / 2;
}
/**
* Calculate the area of a parallelogram.
*
* @param base base of a parallelogram
* @param height height of a parallelogram
* @return area of given parallelogram
*/
public static double surfaceAreaParallelogram(final double baseLength, final double height) {
if (baseLength <= 0) {
throw new IllegalArgumentException(POSITIVE_BASE);
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass baseLength > 0: surfaceAreaTriangle(3, 4).
- Validate base and height before calling.
- Reject degenerate (zero-base) triangles upstream.
Example fix
// before
double a = Area.surfaceAreaTriangle(base, h);
// after
if (base <= 0 || h <= 0) throw new IllegalArgumentException("dimensions must be > 0");
double a = Area.surfaceAreaTriangle(base, h); Defensive patterns
Strategy: validation
Validate before calling
if (!(baseLength > 0) || !(height > 0)) {
throw new IllegalArgumentException("triangle dimensions must be > 0");
}
double a = Area.surfaceAreaTriangle(baseLength, 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.surfaceAreaTriangle(base, h);
} catch (IllegalArgumentException e) {
// base or height non-positive; re-check to identify which
} Prevention
- Pre-validate base and height together for one consolidated error.
- Reject degenerate (zero-base) triangles upstream.
- Watch for base/height derived from coordinate diffs that are 0.
When it happens
Trigger: surfaceAreaTriangle(0, 4); surfaceAreaTriangle(-3, 4); base defaulted to 0.
Common situations: Default 0.0 base field; base computed from a side length that came out 0 or negative.
Related errors
- Side length must be greater than 0
- Length must be greater than 0
- Width must be greater than 0
- Height must be greater than 0
- Radius must be greater than 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/a3e3467ca66db811.
Report an issue: GitHub.