TheAlgorithms/Java · error · IllegalArgumentException
slant height must be greater than 0
Error message
slant height must be greater than 0
What it means
Thrown by Area.surfaceAreaPyramid when slantHeight <= 0, after sideLength has already passed its guard. The message 'slant height must be greater than 0' correctly identifies the field (note the lowercase 'slant', inconsistent with the Title-Case style used elsewhere in the class).
Source
Thrown at src/main/java/com/thealgorithms/maths/Area.java:84
if (radius <= 0) {
throw new IllegalArgumentException(POSITIVE_RADIUS);
}
return 4 * Math.PI * radius * radius;
}
/**
* Calculate the surface area of a pyramid with a square base.
*
* @param sideLength side length of the square base
* @param slantHeight slant height of the pyramid
* @return surface area of the given pyramid
*/
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
if (sideLength <= 0) {
throw new IllegalArgumentException("");
}
if (slantHeight <= 0) {
throw new IllegalArgumentException("slant height must be greater than 0");
}
double baseArea = sideLength * sideLength;
double lateralSurfaceArea = 2 * sideLength * slantHeight;
return baseArea + lateralSurfaceArea;
}
/**
* Calculate the area of a rectangle.
*
* @param length length of a rectangle
* @param width width of a rectangle
* @return area of given rectangle
*/
public static double surfaceAreaRectangle(final double length, final double width) {
if (length <= 0) {
throw new IllegalArgumentException("Length must be greater than 0");
}
if (width <= 0) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass slantHeight > 0: surfaceAreaPyramid(4, 6).
- Validate both pyramid arguments before the call.
- Recompute slant height correctly (slant = sqrt((side/2)^2 + height^2)) rather than forwarding a 0.
Example fix
// before
double a = Area.surfaceAreaPyramid(side, slant);
// after
if (slant <= 0) throw new IllegalArgumentException("slant height must be > 0");
double a = Area.surfaceAreaPyramid(side, slant); Defensive patterns
Strategy: validation
Validate before calling
if (!(slantHeight > 0)) {
throw new IllegalArgumentException("pyramid slant height must be > 0");
}
double a = Area.surfaceAreaPyramid(sideLength, slantHeight); Type guard
static boolean isPositiveDimension(double d) {
return d > 0 && Double.isFinite(d);
} Try / catch
try {
double a = Area.surfaceAreaPyramid(side, slant);
} catch (IllegalArgumentException e) {
if (e.getMessage() != null && e.getMessage().toLowerCase().contains("slant")) { /* slant offender */ }
} Prevention
- Compute slant height correctly from side and vertical height rather than passing 0.
- Validate both pyramid arguments before the call.
- Note the lowercase 'slant' in the message if you match on text.
When it happens
Trigger: surfaceAreaPyramid(4, 0); surfaceAreaPyramid(4, -3); a valid side paired with a zero/negative slant height.
Common situations: Slant height computed from vertical height and side that produced 0/negative via a sqrt or subtraction; default 0.0 field.
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/034a76aac91db6da.
Report an issue: GitHub.