{"record":{"id":"4af429e866d2dbf3","repo":"TheAlgorithms/Java","slug":"no-valid-prime-sum-found","errorCode":null,"errorMessage":"No valid prime sum found.","messagePattern":"No valid prime sum found\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"critical","filePath":"src/main/java/com/thealgorithms/maths/GoldbachConjecture.java","lineNumber":28,"sourceCode":" */\n\npublic final class GoldbachConjecture {\n    private GoldbachConjecture() {\n    }\n    public record Result(int number1, int number2) {\n    }\n\n    public static Result getPrimeSum(int number) {\n        if (number <= 2 || number % 2 != 0) {\n            throw new IllegalArgumentException(\"Number must be even and greater than 2.\");\n        }\n\n        for (int i = 0; i <= number / 2; i++) {\n            if (isPrime(i) && isPrime(number - i)) {\n                return new Result(i, number - i);\n            }\n        }\n        throw new IllegalStateException(\"No valid prime sum found.\"); // Should not occur\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java#L10-L31","documentation":"Thrown as IllegalStateException (not IllegalArgumentException) by GoldbachConjecture.getPrimeSum when the search loop completes without finding two primes that sum to the input. The comment says 'Should not occur' — this is a defensive guard against a counterexample to Goldbach's Conjecture, which has been verified for all even integers up to 4×10^18. If this fires, either the input bypassed the precondition check or there is a bug in isPrime.","triggerScenarios":"This should never fire if the precondition (even number > 2) is met and isPrime is correct. It could fire if: (1) the class is subclassed or modified to skip validation, (2) isPrime has a bug returning false for actual primes, or (3) integer overflow in isPrime's internal arithmetic causes incorrect results. The loop iterates i from 0 to number/2 checking isPrime(i) && isPrime(number - i).","commonSituations":"This is a theoretically-unreachable defensive throw. In practice, encountering it suggests a corrupted or modified isPrime implementation, or that someone changed the precondition guard. It serves as an assertion-style safeguard.","solutions":["Verify that isPrime is correct and unmodified — this is the most likely culprit if the error fires.","Ensure the precondition (number > 2 and even) is always enforced before calling.","If you are extending this class, do not bypass the initial validation check.","Treat this as an assertion failure: investigate the isPrime implementation rather than the caller logic."],"exampleFix":"// This error is defensive/unreachable; no caller-side fix applies.\n// If it fires, investigate isPrime correctness:\n\n// Verify isPrime for known primes near your input\nfor (int i = 2; i <= n / 2; i++) {\n    if (!GoldbachConjecture.isPrime(i)) {\n        System.err.println(\"isPrime bug detected at i=\" + i);\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    var result = GoldbachConjecture.getPrimeSum(number);\n} catch (IllegalStateException e) {\n    // This should never happen for valid even inputs > 2.\n    // If it does, isPrime may be corrupted — investigate immediately.\n    throw new AssertionError(\"Goldbach conjecture violation detected\", e);\n}","preventionTips":["This is a defensive/unreachable guard — if it fires, investigate isPrime correctness.","Do not bypass the precondition check (number > 2, even) in subclasses or wrappers.","Treat this IllegalStateException as an assertion failure, not a normal error path."],"tags":["math","goldbach","prime","illegal-state","defensive-guard","unreachable"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}