{"record":{"id":"233c2433aa95b8df","repo":"doocs/leetcode","slug":"division-by-zero-is-not-allowed-233c24","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/README_EN.md","lineNumber":120,"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```\n\n<!-- tabs:end -->\n","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/2700-2799/2726.Calculator with Method Chaining/README_EN.md#L102-L138","documentation":"English README copy of the LeetCode 2726 Calculator divide guard: if (value === 0) throw new Error('Division by zero is not allowed') before performing this.x /= value and returning this. It converts what would be silent IEEE-754 Infinity into an explicit, catchable failure inside the fluent chain.","triggerScenarios":".divide(0) anywhere in a chain; .divide(n % k) style expressions where the modulo can be 0; divisors read from external input without validation.","commonSituations":"Following the tutorial and testing edge cases; refactoring imperative calculators into chains and forgetting zero handling; demoing fluent interfaces where one bad link kills the chain; mixing string '0' vs number 0 when parsing input (=== is strict).","solutions":["Pre-check value === 0 (and consider '0' after Number()) before calling divide","Restructure the chain so the divisor is validated first","Catch the error around the whole chain and report which step failed","Add unit tests for divide(0) to lock in the intended behavior"],"exampleFix":"// before\ncalc.add(5).divide(0).getResult(); // throws\n\n// after\nif (d === 0) throw new RangeError('bad divisor');\ncalc.add(5).divide(d).getResult();","handlingStrategy":"validation","validationCode":"const d = Number(rawDivisor);\nif (!Number.isFinite(d) || d === 0) throw new RangeError('invalid divisor');\ncalc.divide(d);","typeGuard":"function isSafeDivisor(v: unknown): v is number { return typeof v === 'number' && v !== 0; }","tryCatchPattern":"try { const out = calc.divide(d).getResult(); } catch (e) { if ((e as Error).message.includes('Division by zero')) handleBadDivisor(); else throw e; }","preventionTips":["Check divisors before building chains","Validate at API boundaries","Add divide(0) unit tests"],"tags":["typescript","arithmetic","division-by-zero","documentation"],"backgroundTag":"division-by-zero","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}