{"record":{"id":"0f46d4ccb649bb01","repo":"TheAlgorithms/C-Sharp","slug":"cannot-divide-by-zero","errorCode":null,"errorMessage":"Cannot divide by zero","messagePattern":"Cannot divide by zero","errorType":"exception","errorClass":"DivideByZeroException","httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/InfixToPostfix.cs","lineNumber":164,"sourceCode":"                }\n\n                postfix.Append(stack.Pop());\n            }\n        }\n\n        private static void EvaluateOperator(Stack<int> stack, char op)\n        {\n            if (stack.Count < 2)\n            {\n                throw new InvalidOperationException(\"Insufficient operands\");\n            }\n\n            int b = stack.Pop();\n            int a = stack.Pop();\n\n            if (op == '/' && b == 0)\n            {\n                throw new DivideByZeroException(\"Cannot divide by zero\");\n            }\n\n            int result = op switch\n            {\n                '+' => a + b,\n                '-' => a - b,\n                '*' => a * b,\n                '/' => a / b,\n                '^' => (int)Math.Pow(a, b),\n                _ => throw new InvalidOperationException($\"Unknown operator {op}\"),\n            };\n\n            stack.Push(result);\n        }\n\n        private static void ValidateInfix(string expr)\n        {\n            if (string.IsNullOrEmpty(expr) || string.IsNullOrWhiteSpace(expr))","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/InfixToPostfix.cs#L146-L182","documentation":"EvaluateOperator throws DivideByZeroException when the '/' operator is applied and the second operand (b, the divisor) is 0. The library explicitly checks this instead of letting integer division throw.","triggerScenarios":"Evaluating postfix like \"5 0 /\" where the divisor evaluates to zero.","commonSituations":"Dynamic expressions with computed denominators that happen to be zero; user-supplied expressions dividing by a variable value of 0.","solutions":["Check operands before evaluation, or catch DivideByZeroException around the evaluation call.","Restructure the expression to guard division, or reject zero divisors at input time.","Return a sentinel/nullable result when division by zero is possible in your domain."],"exampleFix":"// before\nint r = evaluator.PostfixExpressionEvaluation(\"5 0 /\"); // throws\n// after\ntry { int r = evaluator.PostfixExpressionEvaluation(\"5 0 /\"); }\ncatch (DivideByZeroException) { /* handle: report invalid expression */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { int r = evaluator.PostfixExpressionEvaluation(postfix); } catch (DivideByZeroException) { /* divisor evaluated to 0; handle gracefully */ }","preventionTips":["Evaluate symbols to numbers first and check for zero divisors","Restructure expressions to avoid literal divisions by zero","Always wrap dynamic expression evaluation in try/catch"],"tags":["division","postfix","arithmetic"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}