{"record":{"id":"27971973ba2d0b6e","repo":"TheAlgorithms/Java","slug":"focal-length-and-object-distance-must-be-non-zero","errorCode":null,"errorMessage":"Focal length and object distance must be non-zero.","messagePattern":"Focal length and object distance must be non-zero\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/ThinLens.java","lineNumber":38,"sourceCode":" */\npublic final class ThinLens {\n\n    private ThinLens() {\n        throw new AssertionError(\"No instances.\");\n    }\n\n    /**\n     * Computes the image distance using the thin lens formula.\n     *\n     * @param focalLength focal length of the lens (f)\n     * @param objectDistance object distance (u)\n     * @return image distance (v)\n     * @throws IllegalArgumentException if focal length or object distance is zero\n     */\n    public static double imageDistance(double focalLength, double objectDistance) {\n\n        if (focalLength == 0 || objectDistance == 0) {\n            throw new IllegalArgumentException(\"Focal length and object distance must be non-zero.\");\n        }\n\n        return 1.0 / ((1.0 / focalLength) - (1.0 / objectDistance));\n    }\n\n    /**\n     * Computes magnification of the image.\n     *\n     * <pre>\n     *     m = v / u\n     * </pre>\n     *\n     * @param imageDistance image distance (v)\n     * @param objectDistance object distance (u)\n     * @return magnification\n     * @throws IllegalArgumentException if object distance is zero\n     */\n    public static double magnification(double imageDistance, double objectDistance) {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/ThinLens.java#L20-L56","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nThinLens.imageDistance(focalLen, objDist); // focalLen or objDist may be 0.0\n\n// after\nif (Double.isFinite(focalLen) && Double.isFinite(objDist) && focalLen != 0.0 && objDist != 0.0) {\n    double v = ThinLens.imageDistance(focalLen, objDist);\n} else {\n    throw new IllegalArgumentException(\"focalLen and objDist must be finite, non-zero doubles\");\n}","handlingStrategy":"validation","validationCode":"if (!Double.isFinite(focalLength) || !Double.isFinite(objectDistance)\n        || focalLength == 0.0 || objectDistance == 0.0) {\n    throw new IllegalArgumentException(\"focalLength and objectDistance must be finite and non-zero\");\n}\ndouble v = ThinLens.imageDistance(focalLength, objectDistance);","typeGuard":"// Java has no structural type narrowing; use a static guard method\nstatic boolean validLensParams(double f, double u) {\n    return Double.isFinite(f) && Double.isFinite(u) && f != 0.0 && u != 0.0;\n}","tryCatchPattern":"try {\n    double v = ThinLens.imageDistance(focalLength, objectDistance);\n} catch (IllegalArgumentException e) {\n    // log optics params at debug, degrade gracefully; do NOT retry with same values\n    logger.warn(\"Invalid lens parameters: f={}, u={}\", focalLength, objectDistance);\n}","preventionTips":["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."],"tags":["physics","input-validation","illegal-argument","thin-lens"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}