{"record":{"id":"7ed05a952ae42d4b","repo":"TheAlgorithms/C-Sharp","slug":"unknown-operator-op","errorCode":null,"errorMessage":"Unknown operator {op}","messagePattern":"Unknown operator (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"Algorithms/Stack/InfixToPostfix.cs","lineNumber":174,"sourceCode":"                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))\n            {\n                throw new ArgumentException(\"Infix cannot be null or empty.\");\n            }\n        }\n\n        private static void ValidatePostfix(string expr)\n        {\n            if (string.IsNullOrEmpty(expr) || string.IsNullOrWhiteSpace(expr))\n            {\n                throw new ArgumentException(\"Postfix cannot be null or empty.\");","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/InfixToPostfix.cs#L156-L192","documentation":"EvaluateOperator throws InvalidOperationException in the switch default arm when the operator character is not one of +, -, *, /, ^. In practice this is unreachable when PostfixExpressionEvaluation routes only IsOperator characters to EvaluateOperator, so hitting it indicates an internal invariant violation or an operator recognized by IsOperator but not handled in the switch.","triggerScenarios":"A character classified as an operator by IsOperator (e.g. '%' if IsOperator accepts it) but missing from the switch expression in EvaluateOperator.","commonSituations":"Library version drift where IsOperator and EvaluateOperator's switch disagree; custom subclasses extending IsOperator without updating the switch.","solutions":["Check IsOperator's accepted characters and confirm each has an arm in the EvaluateOperator switch.","Update the switch to handle the missing operator (e.g. add '%' => a % b).","Catch InvalidOperationException and log the unexpected operator for diagnosis."],"exampleFix":"// before\n'/' => a / b,\n_ => throw new InvalidOperationException($\"Unknown operator {op}\"),\n// after\n'/' => a / b,\n'%' => a % b,\n_ => throw new InvalidOperationException($\"Unknown operator {op}\"),","handlingStrategy":"validation","validationCode":"// ensure expression only uses + - * / ^\nif (postfix.Any(c => \"+-*/^\".Contains(c) == false && !char.IsLetterOrDigit(c))) throw new ArgumentException(\"Unsupported operator present.\");","typeGuard":null,"tryCatchPattern":"try { int r = evaluator.PostfixExpressionEvaluation(postfix); } catch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Unknown operator\")) { /* log: operator recognized by IsOperator but not handled */ }","preventionTips":["Keep IsOperator and EvaluateOperator's switch in sync","Add a test enumerating every character IsOperator accepts","Do not subclass/extend IsOperator without updating the switch"],"tags":["operator","invariant","postfix"],"backgroundTag":"internal-invariant-violation","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"}