{"record":{"id":"69b80bf4710b275f","repo":"TheAlgorithms/C-Sharp","slug":"mismatched-parentheses","errorCode":null,"errorMessage":"Mismatched parentheses.","messagePattern":"Mismatched parentheses\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/InfixToPostfix.cs","lineNumber":145,"sourceCode":"        }\n\n        private static void ProcessOperator(char c, Stack<char> stack, StringBuilder postfixExpression)\n        {\n            while (stack.Count > 0 && stack.Peek() != '(' && Precedence(stack.Peek()) >= Precedence(c))\n            {\n                postfixExpression.Append(stack.Pop());\n            }\n\n            stack.Push(c);\n        }\n\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            {","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/InfixToPostfix.cs#L127-L163","documentation":"EmptyRemainingStack throws InvalidOperationException when leftover items on the operator stack include a parenthesis, meaning a '(' was never closed. The infix expression has an unmatched opening parenthesis.","triggerScenarios":"Calling InfixToPostfixConversion with more '(' than ')', e.g. InfixToPostfixConversion(\"(a+b\") — the trailing '(' remains when draining the stack.","commonSituations":"Expressions assembled programmatically where a branch forgot to emit the closing bracket; partial user input.","solutions":["Validate bracket balance before conversion (e.g. BalancedParenthesesChecker.IsBalanced).","Ensure every '(' has a matching ')' in the input string.","Catch InvalidOperationException around the conversion call and treat as input validation failure."],"exampleFix":"// before\nstring postfix = converter.InfixToPostfixConversion(\"(a+b\");\n// after\nstring postfix = converter.InfixToPostfixConversion(\"(a+b)\");","handlingStrategy":"validation","validationCode":"if (expr.Count(c => c == '(') != expr.Count(c => c == ')')) throw new ArgumentException(\"Unbalanced parentheses.\");","typeGuard":null,"tryCatchPattern":"try { var postfix = converter.InfixToPostfixConversion(expr); } catch (InvalidOperationException) { /* unmatched '(' left on stack */ }","preventionTips":["Count '(' vs ')' before conversion","Validate programmatically built expressions before use","Catch and translate into user-facing validation messages"],"tags":["parentheses","infix","stack"],"backgroundTag":"invalid-argument-format","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"}