{"record":{"id":"4b81aacd54ba756b","repo":"doocs/leetcode","slug":"division-by-zero-is-not-allowed-4b81aa","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.md","lineNumber":119,"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":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/2700-2799/2726.Calculator with Method Chaining/README.md#L101-L137","documentation":"README (Chinese) copy of the LeetCode 2726 Calculator: divide(value) throws Error('Division by zero is not allowed') when value === 0, guarding the chained API from silently producing Infinity/NaN. The chain returns this from every method, so the throw aborts the entire chain before getResult().","triggerScenarios":"new Calculator(10).divide(0).getResult(); or a computed divisor that evaluates to 0 mid-chain (e.g. after subtract(x) that zeroes the stored value used as the divisor elsewhere).","commonSituations":"Practicing method chaining with edge inputs; divisors sourced from parsed user input defaulting to 0; misunderstanding that the guard uses === so '0' (string) would not throw but 0 does (and vice versa in JS 0 == '0'); building fluent APIs and studying where guards belong.","solutions":["Validate the divisor before chaining: skip or substitute when 0","Check intermediate chain values that feed the divisor","Wrap chains in try/catch when zero is possible at runtime","Parse and validate numeric inputs at the API boundary"],"exampleFix":"// before\nnew Calculator(20).divide(0).getResult(); // throws\n\n// after\nconst c = new Calculator(20);\nconst r = d !== 0 ? c.divide(d).getResult() : NaN;","handlingStrategy":"validation","validationCode":"if (d === 0) { /* choose fallback */ } else { calc.divide(d); }","typeGuard":"const isNonZeroNumber = (v: unknown): v is number => typeof v === 'number' && v !== 0;","tryCatchPattern":"try { calc.add(10).divide(d).getResult(); } catch (e) { if ((e as Error).message === 'Division by zero is not allowed') { /* fallback */ } else throw e; }","preventionTips":["Validate computed divisors","Test chains with 0","Parse inputs with Number() and check"],"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"}