TheAlgorithms/Java · error · IllegalArgumentException
Focal length and object distance must be non-zero.
Error message
Focal length and object distance must be non-zero.
What it means
Thrown by ThinLens.imageDistance(focalLength, objectDistance) when either focalLength or objectDistance is exactly 0.0. The thin-lens formula computes v = 1 / ((1/f) - (1/u)); a zero in either term makes 1/f or 1/u evaluate to Infinity, producing a meaningless (NaN/Infinity) image distance, so the guard rejects it up front.
Source
Thrown at src/main/java/com/thealgorithms/physics/ThinLens.java:38
*/
public final class ThinLens {
private ThinLens() {
throw new AssertionError("No instances.");
}
/**
* Computes the image distance using the thin lens formula.
*
* @param focalLength focal length of the lens (f)
* @param objectDistance object distance (u)
* @return image distance (v)
* @throws IllegalArgumentException if focal length or object distance is zero
*/
public static double imageDistance(double focalLength, double objectDistance) {
if (focalLength == 0 || objectDistance == 0) {
throw new IllegalArgumentException("Focal length and object distance must be non-zero.");
}
return 1.0 / ((1.0 / focalLength) - (1.0 / objectDistance));
}
/**
* Computes magnification of the image.
*
* <pre>
* m = v / u
* </pre>
*
* @param imageDistance image distance (v)
* @param objectDistance object distance (u)
* @return magnification
* @throws IllegalArgumentException if object distance is zero
*/
public static double magnification(double imageDistance, double objectDistance) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Initialize focalLength and objectDistance to a non-zero physical value (e.g. 10.0) before calling.
- If the value comes from input, validate focalLength != 0 && objectDistance != 0 and surface a clear error before invoking imageDistance.
- Guard against NaN/Infinity too if you accept computed values: Double.isFinite(focalLength) && Double.isFinite(objectDistance) && focalLength != 0 && objectDistance != 0.
Example fix
// before
ThinLens.imageDistance(focalLen, objDist); // focalLen or objDist may be 0.0
// after
if (Double.isFinite(focalLen) && Double.isFinite(objDist) && focalLen != 0.0 && objDist != 0.0) {
double v = ThinLens.imageDistance(focalLen, objDist);
} else {
throw new IllegalArgumentException("focalLen and objDist must be finite, non-zero doubles");
} Defensive patterns
Strategy: validation
Validate before calling
if (!Double.isFinite(focalLength) || !Double.isFinite(objectDistance)
|| focalLength == 0.0 || objectDistance == 0.0) {
throw new IllegalArgumentException("focalLength and objectDistance must be finite and non-zero");
}
double v = ThinLens.imageDistance(focalLength, objectDistance); Type guard
// Java has no structural type narrowing; use a static guard method
static boolean validLensParams(double f, double u) {
return Double.isFinite(f) && Double.isFinite(u) && f != 0.0 && u != 0.0;
} Try / catch
try {
double v = ThinLens.imageDistance(focalLength, objectDistance);
} catch (IllegalArgumentException e) {
// log optics params at debug, degrade gracefully; do NOT retry with same values
logger.warn("Invalid lens parameters: f={}, u={}", focalLength, objectDistance);
} Prevention
- Never use 0.0 as an 'unset' sentinel for optical parameters — use Double.NaN or Optional<Double>.
- Validate at the trust boundary (input/config parse) not at the call site deep in physics code.
- Pair focalLength and objectDistance in a small value object whose constructor enforces the invariant.
When it happens
Trigger: Call imageDistance(0.0, 10.0), imageDistance(5.0, 0.0), or imageDistance(0.0, 0.0). Any call where focalLength == 0 || objectDistance == 0 trips it. Note it does NOT reject negatives or NaN — only exact 0.0.
Common situations: Defaulting an optical parameter to 0 as an 'unset' sentinel; parsing a focal length from user input or a config file that defaulted to 0; physics simulations where a lens/object wasn't initialized before the first frame.
Related errors
- Object distance must be non-zero.
- Input '" + input + "' contains not only digits
- Orbiting mass and radius must be positive.
- Natural frequency must be positive.
- Damping coefficient must be non-negative.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/27971973ba2d0b6e.
Report an issue: GitHub.