{"record":{"id":"00635d53dcc493ce","repo":"EnterpriseQualityCoding/FizzBuzzEnterpriseEdition","slug":"an-attempt-was-made-to-divide-by-zero","errorCode":null,"errorMessage":"An attempt was made to divide by zero.","messagePattern":"An attempt was made to divide by zero\\.","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/math/arithmetics/IntegerDivider.java","lineNumber":43,"sourceCode":"\t */\n\t@Autowired\n\tpublic IntegerDivider(final FirstIsLargerThanSecondDoubleComparator firstIsLargerThanSecondDoubleComparator,\n\t\t\tfinal FirstIsSmallerThanSecondDoubleComparator firstIsSmallerThanSecondDoubleComparator) {\n\t\tsuper();\n\t\tthis.firstIsLargerThanSecondDoubleComparator = firstIsLargerThanSecondDoubleComparator;\n\t\tthis.firstIsSmallerThanSecondDoubleComparator = firstIsSmallerThanSecondDoubleComparator;\n\t}\n\n\t/**\n\t * @param nFirstInteger int\n\t * @param nSecondInteger int\n\t * @return int\n\t */\n\tpublic int divide(final int nFirstInteger, final int nSecondInteger) {\n\t\tfinal boolean denominatorEqualsZero =\n\t\t\t\tIntegerForEqualityComparator.areTwoIntegersEqual(nSecondInteger, Constants.INTEGER_DIVIDE_ZERO_VALUE);\n\t\tif (denominatorEqualsZero) {\n\t\t\tthrow new ArithmeticException(Constants.AN_ATTEMPT_WAS_MADE_TO_DIVIDE_BY_ZERO);\n\t\t} else {\n\t\t\tfinal double dbFirstNumber = IntToDoubleConverter.Convert(nFirstInteger);\n\t\t\tfinal double dbSecondNumber = IntToDoubleConverter.Convert(nSecondInteger);\n\t\t\tfinal double dbQuotient = dbFirstNumber / dbSecondNumber;\n\t\t\tdouble dbRoundedQuotient = (double) Constants.INTEGER_ORIGIN_ZERO_VALUE;\n\t\t\tif (this.firstIsSmallerThanSecondDoubleComparator.FirstIsSmallerThanSecond(dbQuotient,\n\t\t\t\t\t(double) Constants.INTEGER_ORIGIN_ZERO_VALUE)) {\n\t\t\t\tdbRoundedQuotient = Math.ceil(dbQuotient);\n\t\t\t} else if (this.firstIsLargerThanSecondDoubleComparator.FirstIsLargerThanSecond(dbQuotient,\n\t\t\t\t\t(double) Constants.INTEGER_ORIGIN_ZERO_VALUE)) {\n\t\t\t\tdbRoundedQuotient = Math.floor(dbQuotient);\n\t\t\t}\n\t\t\tfinal int nIntegerQuotient = DoubleToIntConverter.Convert(dbRoundedQuotient);\n\t\t\treturn nIntegerQuotient;\n\t\t}\n\t}\n\n}","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition/blob/4922c077c07a3744ae67ee8a932786f15bf57411/src/main/java/com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/math/arithmetics/IntegerDivider.java#L25-L61","documentation":"This is the IntegerDivider from the FizzBuzz Enterprise Edition satire codebase. Before dividing, it explicitly checks whether the denominator equals zero (via IntegerForEqualityComparator against Constants.INTEGER_DIVIDE_ZERO_VALUE) and throws a plain java.lang.ArithmeticException with the message 'An attempt was made to divide by zero'. Although the actual quotient is computed with doubles (which would yield Infinity/NaN rather than throw), the library chooses to fail fast with the classic exception instead of propagating non-finite values.","triggerScenarios":"Calling IntegerDivider.divide(nFirstInteger, nSecondInteger) (injected via its constructor with the two DoubleComparators) with nSecondInteger == 0, e.g. divide(15, 0) or divide(0, 0). The equality comparator compares nSecondInteger to Constants.INTEGER_DIVIDE_ZERO_VALUE (0); any other denominator proceeds to double division.","commonSituations":"In this codebase the divider is used by LoopComponent/@Invalid gitBranchList currentStepRange computations where the loop range (nFizzBuzzLow, nFizzBuzzHigh) is derived from configuration: a zero FizzBuzzHigh (or a misconfigured range constant) makes every loop iteration hit this. Generally: computing ratios/percentages from a denominator that can legitimately be zero (counts, totals, batch sizes) without pre-checking.","solutions":["Check the denominator before calling divide() and decide policy (skip, clamp, or report) at the call site.","If a zero denominator is expected input, guard the caller: if (denominator == 0) { /* handle */ } else { divider.divide(num, denominator); }","If the value comes from config (loop range), validate the configuration at startup so Constants/range values cannot be zero.","As a last resort, wrap the call in try { ... } catch (ArithmeticException e) { ... } — but pre-checking is cleaner since the condition is trivially testable."],"exampleFix":"// before\nfinal int quotient = this.integerDivider.divide(numerator, denominator);\n\n// after\nif (denominator == 0) {\n    throw new IllegalArgumentException(\"denominator must be non-zero\");\n}\nfinal int quotient = this.integerDivider.divide(numerator, denominator);","handlingStrategy":"validation","validationCode":"// Run before IntegerDivider.divide(...)\nif (nSecondInteger == 0) {\n    // choose your policy: skip, clamp, or fail with context\n    throw new IllegalArgumentException(\"denominator must be non-zero, got: \" + nSecondInteger);\n}\nfinal int result = integerDivider.divide(nFirstInteger, nSecondInteger);","typeGuard":null,"tryCatchPattern":"// Only as a boundary guard around third-party-driven denominators\ntry {\n    final int q = integerDivider.divide(n, d);\n} catch (ArithmeticException e) {\n    if (!\"An attempt was made to divide by zero.\".equals(e.getMessage())) throw e;\n    logger.warn(\"Skipped zero-denominator division for n={}\", n);\n}","preventionTips":["Validate denominators at the call site before divide(); the check is one == comparison.","Validate loop-range/config constants at startup so denominators derived from them can never be zero.","Never rely on the double-division inside the library to 'absorb' zero — this implementation fails fast on purpose.","Centralize division in one wrapper that enforces the zero-check policy, instead of checking at every call site."],"tags":["java","arithmetic","division-by-zero","fizzbuzz-enterprise","validation"],"backgroundTag":null,"analyzedSha":"4922c077c07a3744ae67ee8a932786f15bf57411","analyzedAt":"2026-08-14T10:51:26.690Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}