{"record":{"id":"b9da9d6d4161853a","repo":"TheAlgorithms/C-Sharp","slug":"mismatched-parentheses-in-expression","errorCode":null,"errorMessage":"Mismatched parentheses in expression.","messagePattern":"Mismatched parentheses in expression\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/InfixToPostfix.cs","lineNumber":123,"sourceCode":"            if (c == ')')\n            {\n                ProcessClosingParenthesis(stack, postfixExpression);\n                return;\n            }\n\n            ProcessOperator(c, stack, postfixExpression);\n        }\n\n        private static void ProcessClosingParenthesis(Stack<char> stack, StringBuilder postfixExpression)\n        {\n            while (stack.Count > 0 && stack.Peek() != '(')\n            {\n                postfixExpression.Append(stack.Pop());\n            }\n\n            if (stack.Count == 0)\n            {\n                throw new InvalidOperationException(\"Mismatched parentheses in expression.\");\n            }\n\n            stack.Pop();\n        }\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)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/InfixToPostfix.cs#L105-L141","documentation":"ProcessClosingParenthesis throws InvalidOperationException when a ')' is encountered but the operator stack is empty, meaning there is no matching '('. The infix expression has an unbalanced closing parenthesis.","triggerScenarios":"Calling InfixToPostfixConversion with more ')' than '(', e.g. InfixToPostfixConversion(\"a+b)\").","commonSituations":"Manually typed expressions missing an opening bracket; string building code that appends closing parens conditionally.","solutions":["Balance-check the infix expression (e.g. with BalancedParenthesesChecker.IsBalanced) before conversion.","Fix the expression so every ')' has a preceding '('.","Catch InvalidOperationException and report mismatched parentheses to the user."],"exampleFix":"// before\nstring postfix = converter.InfixToPostfixConversion(\"(a+b))\");\n// after\nstring expr = \"(a+b))\";\nif (checker.IsBalanced(expr)) { var postfix = converter.InfixToPostfixConversion(expr); }","handlingStrategy":"validation","validationCode":"int depth = 0; foreach (var c in expr) { if (c == '(') depth++; else if (c == ')' && --depth < 0) throw new ArgumentException(\"Unbalanced ')'.\"); } if (depth != 0) throw new ArgumentException(\"Unbalanced '('.\");","typeGuard":null,"tryCatchPattern":"try { var postfix = converter.InfixToPostfixConversion(expr); } catch (InvalidOperationException) { /* mismatched parentheses */ }","preventionTips":["Run a balance check before every conversion","Lint user input for unbalanced brackets","Reuse BalancedParenthesesChecker.IsBalanced as a pre-check"],"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"}