{"record":{"id":"021dd4b991e8744e","repo":"TheAlgorithms/C-Sharp","slug":"invalid-character-c-found-in-the-expression","errorCode":null,"errorMessage":"Invalid character '{c}' found in the expression.","messagePattern":"Invalid character '(.+?)' found in the expression\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/BalancedParenthesesChecker.cs","lineNumber":54,"sourceCode":"        }\n\n        Stack<char> stack = new Stack<char>();\n        foreach (char c in expression)\n        {\n            if (IsOpeningParenthesis(c))\n            {\n                stack.Push(c);\n            }\n            else if (IsClosingParenthesis(c))\n            {\n                if (!IsBalancedClosing(stack, c))\n                {\n                    return false;\n                }\n            }\n            else\n            {\n                throw new ArgumentException($\"Invalid character '{c}' found in the expression.\");\n            }\n        }\n\n        return stack.Count == 0;\n    }\n\n    private static bool IsOpeningParenthesis(char c)\n    {\n        return c == '(' || c == '{' || c == '[';\n    }\n\n    private static bool IsClosingParenthesis(char c)\n    {\n        return c == ')' || c == '}' || c == ']';\n    }\n\n    private static bool IsBalancedClosing(Stack<char> stack, char close)\n    {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/BalancedParenthesesChecker.cs#L36-L72","documentation":"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.","triggerScenarios":"Calling IsBalanced with an expression containing characters outside the recognized bracket set, e.g. IsBalanced(\"(a[b]{c})\") or IsBalanced(\"<>\").","commonSituations":"Users pass raw source-code snippets, math expressions with operators/letters, or XML-style angle brackets instead of a pure bracket-only string.","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."],"exampleFix":"// before\nbool ok = checker.IsBalanced(\"(a + b) * [c]\");\n// after\nstring bracketsOnly = Regex.Replace(\"(a + b) * [c]\", \"[^()\\[\\]{}]\", \"\");\nbool ok = checker.IsBalanced(bracketsOnly);","handlingStrategy":"validation","validationCode":"if (Regex.IsMatch(expression, \"[^()\\[\\]{}]\")) throw new ArgumentException(\"Expression must contain only ()[]{} characters.\");","typeGuard":"bool IsBracketOnly(string s) => !string.IsNullOrEmpty(s) && s.All(c => c is '(' or ')' or '[' or ']' or '{' or '}');","tryCatchPattern":"try { checker.IsBalanced(expr); } catch (ArgumentException ex) { /* show ex.Message as input validation error */ }","preventionTips":["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"],"tags":["validation","stack","argument"],"backgroundTag":"invalid-argument-value","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"}