TheAlgorithms/C-Sharp · warning · InvalidOperationException

Unknown operator

Error message

Unknown operator {op}

What it means

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.

Solutions

  1. Check IsOperator's accepted characters and confirm each has an arm in the EvaluateOperator switch.
  2. Update the switch to handle the missing operator (e.g. add '%' => a % b).
  3. Catch InvalidOperationException and log the unexpected operator for diagnosis.

Example fix

// before
'/' => a / b,
_ => throw new InvalidOperationException($"Unknown operator {op}"),
// after
'/' => a / b,
'%' => a % b,
_ => throw new InvalidOperationException($"Unknown operator {op}"),
Defensive patterns

Strategy: validation

Validate before calling

// ensure expression only uses + - * / ^
if (postfix.Any(c => "+-*/^".Contains(c) == false && !char.IsLetterOrDigit(c))) throw new ArgumentException("Unsupported operator present.");

Try / catch

try { int r = evaluator.PostfixExpressionEvaluation(postfix); } catch (InvalidOperationException ex) when (ex.Message.StartsWith("Unknown operator")) { /* log: operator recognized by IsOperator but not handled */ }

Prevention

When it happens

Trigger: A character classified as an operator by IsOperator (e.g. '%' if IsOperator accepts it) but missing from the switch expression in EvaluateOperator.

Common situations: Library version drift where IsOperator and EvaluateOperator's switch disagree; custom subclasses extending IsOperator without updating the switch.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/7ed05a952ae42d4b. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Stack/InfixToPostfix.cs:174

                throw new InvalidOperationException("Insufficient operands");
            }

            int b = stack.Pop();
            int a = stack.Pop();

            if (op == '/' && b == 0)
            {
                throw new DivideByZeroException("Cannot divide by zero");
            }

            int result = op switch
            {
                '+' => a + b,
                '-' => a - b,
                '*' => a * b,
                '/' => a / b,
                '^' => (int)Math.Pow(a, b),
                _ => throw new InvalidOperationException($"Unknown operator {op}"),
            };

            stack.Push(result);
        }

        private static void ValidateInfix(string expr)
        {
            if (string.IsNullOrEmpty(expr) || string.IsNullOrWhiteSpace(expr))
            {
                throw new ArgumentException("Infix cannot be null or empty.");
            }
        }

        private static void ValidatePostfix(string expr)
        {
            if (string.IsNullOrEmpty(expr) || string.IsNullOrWhiteSpace(expr))
            {
                throw new ArgumentException("Postfix cannot be null or empty.");

View on GitHub (pinned to 96e2905cab)