TheAlgorithms/C-Sharp · error · ArgumentException
Invalid character ' ' found in the expression.
Error message
Invalid character '{c}' found in the expression. What it means
BalancedParenthesesChecker.IsBalanced throws ArgumentException when the expression contains a character that is neither an opening nor a closing parenthesis/bracket/brace. The library only recognizes known bracket symbols, so any other character makes the input undefined for the checker and it fails fast rather than silently ignoring it.
Solutions
- Strip non-bracket characters from the input before calling IsBalanced, e.g. Regex.Replace(input, "[^()\[\]{}]", "").
- Wrap the call in a try/catch for ArgumentException and treat the message as user-facing validation feedback.
- If letters/operators are legitimate input, pre-validate or use an expression parser suited to that grammar instead of this checker.
Example fix
// before
bool ok = checker.IsBalanced("(a + b) * [c]");
// after
string bracketsOnly = Regex.Replace("(a + b) * [c]", "[^()\[\]{}]", "");
bool ok = checker.IsBalanced(bracketsOnly); Defensive patterns
Strategy: validation
Validate before calling
if (Regex.IsMatch(expression, "[^()\[\]{}]")) throw new ArgumentException("Expression must contain only ()[]{} characters."); Type guard
bool IsBracketOnly(string s) => !string.IsNullOrEmpty(s) && s.All(c => c is '(' or ')' or '[' or ']' or '{' or '}'); Try / catch
try { checker.IsBalanced(expr); } catch (ArgumentException ex) { /* show ex.Message as input validation error */ } Prevention
- Strip non-bracket characters with a regex before checking
- Unit-test with realistic inputs containing letters/operators
- Document that the checker only accepts bracket-only strings
When it happens
Trigger: Calling IsBalanced with an expression containing characters outside the recognized bracket set, e.g. IsBalanced("(a[b]{c})") or IsBalanced("<>").
Common situations: Users pass raw source-code snippets, math expressions with operators/letters, or XML-style angle brackets instead of a pure bracket-only string.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Invalid character .
- message
- key must be non-empty string
- k must be at least 1.
- Input features cannot be empty.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/021dd4b991e8744e.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Stack/BalancedParenthesesChecker.cs:54
}
Stack<char> stack = new Stack<char>();
foreach (char c in expression)
{
if (IsOpeningParenthesis(c))
{
stack.Push(c);
}
else if (IsClosingParenthesis(c))
{
if (!IsBalancedClosing(stack, c))
{
return false;
}
}
else
{
throw new ArgumentException($"Invalid character '{c}' found in the expression.");
}
}
return stack.Count == 0;
}
private static bool IsOpeningParenthesis(char c)
{
return c == '(' || c == '{' || c == '[';
}
private static bool IsClosingParenthesis(char c)
{
return c == ')' || c == '}' || c == ']';
}
private static bool IsBalancedClosing(Stack<char> stack, char close)
{View on GitHub (pinned to 96e2905cab)