{"record":{"id":"a1a06533e0ef9d4b","repo":"TheAlgorithms/C-Sharp","slug":"insufficient-operands","errorCode":null,"errorMessage":"Insufficient operands","messagePattern":"Insufficient operands","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/InfixToPostfix.cs","lineNumber":156,"sourceCode":"\n        private static void EmptyRemainingStack(Stack<char> stack, StringBuilder postfix)\n        {\n            while (stack.Count > 0)\n            {\n                if (stack.Peek() is '(' or ')')\n                {\n                    throw new InvalidOperationException(\"Mismatched parentheses.\");\n                }\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}\"),","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/InfixToPostfix.cs#L138-L174","documentation":"EvaluateOperator throws InvalidOperationException when an operator is applied but the operand stack holds fewer than two values. A postfix expression is only evaluable if each operator has two preceding operands.","triggerScenarios":"Evaluating \"2 +\" or \"+\" — an operator appears before enough operands were pushed.","commonSituations":"Malformed postfix generated by buggy converters; expressions truncated during transmission; non-postfix input passed to the evaluator.","solutions":["Verify the expression is valid postfix (operands count = operators count + 1) before evaluation.","Produce postfix via InfixToPostfixConversion instead of hand-writing it.","Catch InvalidOperationException and report the malformed expression."],"exampleFix":"// before\nint r = evaluator.PostfixExpressionEvaluation(\"2 +\");\n// after\nint r = evaluator.PostfixExpressionEvaluation(\"2 3 +\");","handlingStrategy":"validation","validationCode":"int operands = postfix.Count(char.IsLetterOrDigit); int operators = postfix.Count(c => \"+-*/^\".Contains(c)); if (operands != operators + 1) throw new ArgumentException(\"Not a valid postfix expression.\");","typeGuard":null,"tryCatchPattern":"try { int r = evaluator.PostfixExpressionEvaluation(postfix); } catch (InvalidOperationException) { /* insufficient operands */ }","preventionTips":["Verify postfix shape before evaluation","Use the library's own converter to produce postfix","Avoid hand-written postfix strings in production code"],"tags":["postfix","stack","evaluation"],"backgroundTag":"invalid-state-transition","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"}