TheAlgorithms/C-Sharp · error · InvalidOperationException

Invalid character in expression

Error message

Invalid character in expression: {ch}

What it means

PostfixExpressionEvaluation throws InvalidOperationException when a character in the postfix string is neither an operand (digit/letter) nor a known operator, so the evaluator cannot classify it. This signals a malformed or non-postfix expression.

Solutions

  1. Verify the input is valid postfix notation before calling the evaluator (use InfixToPostfixConversion to produce it).
  2. Strip or reject unsupported characters (spaces, commas) before evaluation.
  3. Catch InvalidOperationException and report the offending character position to the user.

Example fix

// before
int r = evaluator.PostfixExpressionEvaluation("2 3 + ,");
// after
string cleaned = "2 3 + ,".Replace(",", "").Replace(" ", "");
int r = evaluator.PostfixExpressionEvaluation(cleaned);
Defensive patterns

Strategy: validation

Validate before calling

if (postfix.Any(c => !char.IsLetterOrDigit(c) && !"+-*/^".Contains(c))) throw new ArgumentException("Postfix contains non-operand/operator characters.");

Type guard

bool IsValidPostfixAlphabet(string s) => s.All(c => char.IsLetterOrDigit(c) || "+-*/^".Contains(c));

Try / catch

try { int r = evaluator.PostfixExpressionEvaluation(postfix); } catch (InvalidOperationException ex) { /* report ex.Message */ }

Prevention

When it happens

Trigger: Evaluating a postfix string containing characters outside the operand/operator set, e.g. PostfixExpressionEvaluation("2 3 + #") or an infix string accidentally passed to the postfix evaluator.

Common situations: Feeding infix notation ("2+3") to the postfix evaluator; expressions with spaces, commas, or decimal points that are not recognized as operands.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at Algorithms/Stack/InfixToPostfix.cs:80

            {
                if(char.IsWhiteSpace(ch))
                {
                    continue;
                }

                if(char.IsDigit(ch))
                {
                    stack.Push(ch - '0');
                    continue;
                }

                if (IsOperator(ch))
                {
                    EvaluateOperator(stack, ch);
                    continue;
                }

                throw new InvalidOperationException($"Invalid character in expression: {ch}");
            }

            if (stack.Count != 1)
            {
                throw new InvalidOperationException("Invalid postfix expression: Leftover operands.");
            }

            return stack.Pop();
        }

        private static void ProcessInfixCharacter(char c, Stack<char> stack, StringBuilder postfixExpression)
        {
            if (IsOperand(c))
            {
                postfixExpression.Append(c);
                return;
            }

View on GitHub (pinned to 96e2905cab)