{"record":{"id":"187743cb1b1ace4c","repo":"TheAlgorithms/Java","slug":"total-internal-reflection-no-refraction-possible","errorCode":null,"errorMessage":"Total internal reflection: no refraction possible.","messagePattern":"Total internal reflection: no refraction possible\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"src/main/java/com/thealgorithms/physics/SnellLaw.java","lineNumber":28,"sourceCode":"    private SnellLaw() {\n        throw new AssertionError(\"No instances.\");\n    }\n\n    /**\n     * Computes the refracted angle (theta2) in radians.\n     *\n     * @param n1 index of refraction of medium 1\n     * @param n2 index of refraction of medium 2\n     * @param theta1 incident angle in radians\n     * @return refracted angle (theta2) in radians\n     * @throws IllegalArgumentException if total internal reflection occurs\n     */\n    public static double refractedAngle(double n1, double n2, double theta1) {\n        double ratio = n1 / n2;\n        double sinTheta2 = ratio * Math.sin(theta1);\n\n        if (Math.abs(sinTheta2) > 1.0) {\n            throw new IllegalArgumentException(\"Total internal reflection: no refraction possible.\");\n        }\n\n        return Math.asin(sinTheta2);\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/SnellLaw.java#L10-L34","documentation":"Thrown by SnellLaw.refractedAngle when the computed sin(theta2) has absolute value greater than 1.0. By Snell's law n1*sin(theta1) = n2*sin(theta2); when light travels from a denser to a rarer medium (n1 > n2) beyond the critical angle, sin(theta2) exceeds 1 and no refraction is possible — the light undergoes total internal reflection. Math.asin would otherwise return NaN for such input.","triggerScenarios":"Calling refractedAngle(n1, n2, theta1) where (n1/n2) * |sin(theta1)| > 1.0 — i.e. n1 > n2 and the incident angle exceeds the critical angle theta_c = asin(n2/n1). Example: n1=1.5 (glass), n2=1.0 (air), theta1 > ~41.8 degrees.","commonSituations":"Optics simulation where the incident angle crosses the critical angle, ray tracing from glass-to-air or water-to-air, or swapping n1 and n2 by mistake so that the denser medium is treated as the destination.","solutions":["Handle total internal reflection as a legitimate physical outcome: catch the exception and reflect the ray instead of refracting it.","Check (n1/n2)*Math.abs(Math.sin(theta1)) <= 1.0 before calling, and branch to a reflection path if it exceeds 1.","Verify n1 and n2 are in the correct order — n1 is the medium the ray is leaving, n2 the medium it is entering.","Confirm theta1 is in radians (convert with Math.toRadians if your input is in degrees)."],"exampleFix":"// before\ndouble theta2 = SnellLaw.refractedAngle(1.5, 1.0, Math.toRadians(50)); // TIR -> throws\n// after\n double ratio = 1.5 / 1.0;\ndouble s2 = ratio * Math.abs(Math.sin(Math.toRadians(50)));\ndouble theta2;\nif (s2 > 1.0) {\n    theta2 = Math.PI / 2 - Math.toRadians(50); // reflect instead\n} else {\n    theta2 = SnellLaw.refractedAngle(1.5, 1.0, Math.toRadians(50));\n}","handlingStrategy":"try-catch","validationCode":"double sinTheta2 = (n1 / n2) * Math.abs(Math.sin(theta1));\ndouble theta2;\nif (sinTheta2 > 1.0) {\n    // total internal reflection — reflect instead of refract\n    theta2 = Math.PI / 2 - theta1;\n} else {\n    theta2 = SnellLaw.refractedAngle(n1, n2, theta1);\n}","typeGuard":null,"tryCatchPattern":"try {\n    theta2 = SnellLaw.refractedAngle(n1, n2, theta1);\n} catch (IllegalArgumentException e) {\n    // total internal reflection: route the ray through reflection logic\n    theta2 = Math.PI / 2 - theta1;\n}","preventionTips":["Pre-compute the critical angle theta_c = asin(n2/n1) and branch before the call when n1 > n2.","Confirm n1 (source medium) and n2 (destination medium) are not swapped.","Ensure theta1 is in radians — convert with Math.toRadians if degrees.","Treat the exception as an expected physical outcome (reflection), not a programming error."],"tags":["java","physics","optics","snells-law","total-internal-reflection","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}