{"record":{"id":"a7106ed762738f52","repo":"doocs/leetcode","slug":"division-by-zero-is-not-allowed","errorCode":null,"errorMessage":"Division by zero is not allowed","messagePattern":"Division by zero is not allowed","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"solution/2700-2799/2726.Calculator with Method Chaining/Solution.ts","lineNumber":25,"sourceCode":"\n    add(value: number): Calculator {\n        this.x += value;\n        return this;\n    }\n\n    subtract(value: number): Calculator {\n        this.x -= value;\n        return this;\n    }\n\n    multiply(value: number): Calculator {\n        this.x *= value;\n        return this;\n    }\n\n    divide(value: number): Calculator {\n        if (value === 0) {\n            throw new Error('Division by zero is not allowed');\n        }\n        this.x /= value;\n        return this;\n    }\n\n    power(value: number): Calculator {\n        this.x **= value;\n        return this;\n    }\n\n    getResult(): number {\n        return this.x;\n    }\n}\n","sourceCodeStart":7,"sourceCodeEnd":40,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/2700-2799/2726.Calculator with Method Chaining/Solution.ts#L7-L40","documentation":"In the LeetCode 2726 Calculator with method chaining, divide(value) guards against dividing by zero and throws Error('Division by zero is not allowed'). This is a deliberate domain guard because JavaScript/TypeScript would otherwise silently return Infinity (or NaN for 0/0), corrupting the chained result.","triggerScenarios":"Calling new Calculator(...).divide(0), or chaining .divide(x) where x computes to 0 (e.g. a subtraction or modulo that yields 0 earlier in the chain).","commonSituations":"Chained expressions like calc.add(10).divide(0).getResult(); deriving the divisor from user input or previous operations without checking; porting code from languages where division by zero throws natively and forgetting the guard exists here too.","solutions":["Check the divisor before calling divide: skip, clamp, or substitute a safe value","Trace earlier chain steps: multiply(0) or subtract-to-zero feeding divide is a common chain bug","Wrap the whole chain in try/catch if zero is a legitimate runtime input","Validate inputs at the boundary (parse/validate user numbers before building the chain)"],"exampleFix":"// before\ncalc.add(10).divide(0).getResult(); // throws\n\n// after\nconst d = getDivisor();\nconst result = d === 0 ? NaN : calc.add(10).divide(d).getResult();","handlingStrategy":"validation","validationCode":"const divisor = computeDivisor();\nif (divisor === 0) { /* skip/clamp */ } else { calc.divide(divisor); }","typeGuard":"const isSafeDivisor = (n: unknown): n is number => typeof n === 'number' && Number.isFinite(n) && n !== 0;","tryCatchPattern":"try { const r = calc.add(10).divide(d).getResult(); } catch (e) { if ((e as Error).message.includes('Division by zero')) { /* handle */ } else throw e; }","preventionTips":["Validate divisors from user input","Watch chain steps that can produce 0","Unit-test divide(0)"],"tags":["typescript","arithmetic","division-by-zero","guard-clause"],"backgroundTag":"division-by-zero","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}