{"record":{"id":"d5b4910fbfbb411a","repo":"dotnet/wpf","slug":"system-argumentexception","errorCode":null,"errorMessage":"System.ArgumentException","messagePattern":"System\\.ArgumentException","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WordBreaker.cs","lineNumber":219,"sourceCode":"\n        public void Move(LogicalDirection direction)\n        {\n            // this should never get called.\n            throw new NotImplementedException();\n        }\n\n        public void MoveToPosition(TextPosition position)\n        {\n            _position = position.Position;\n        }\n\n        public void MoveByDistance(int distance)\n        {\n            // throw ArgumentException if position would be moved out of bounds.\n            int newPosition = _position + distance;\n            if (newPosition < 0 || newPosition > _container.Text.Length)\n            {\n                throw new System.ArgumentException();\n            }\n\n            _position = newPosition;\n        }\n    }\n\n    // the following additional Avalon Text types are required so the WordBreaker class will compile as-is.\n    internal delegate void TextContainerChangedEventHandler(object target, TextContainerChangedEventArgs args);\n    internal class TextContainerChangedEventArgs \n    {\n        // To appease the FxCop\n        private TextContainerChangedEventArgs()\n        { }\n    }\n    internal class InlineElement\n    {\n        // To appease the FxCop\n        private InlineElement()","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WordBreaker.cs#L201-L237","documentation":"WordBreaker.MoveByDistance throws System.ArgumentException when the requested move would push the breaker's position outside the text container: newPosition < 0 or > _container.Text.Length. The class comment states this explicitly — it is an out-of-bounds move guard for the text-breaking iterator.","triggerScenarios":"Calling MoveByDistance(distance) where _position + distance is negative or beyond Text.Length. Internally reachable via MoveNaviForward/MoveNaviBackward/GenerateText/GeneratePosition/BreakText when callers pass distances computed from mismatched text lengths (e.g. text changed between calls).","commonSituations":"Line-breaking/word-wrapping code that advances by characters after the text was edited; callers passing a byte vs char count mismatch; wrapped text measuring code that moves by more than the remaining length.","solutions":["Clamp distance so _position + distance stays within [0, Text.Length] before calling","Check for end-of-text before requesting further moves (compare position to Text.Length)","Catch ArgumentException as the signal that iteration is past the end and stop breaking","Recreate the WordBreaker against the current text if the text changed mid-iteration"],"exampleFix":"// before\nbreaker.MoveByDistance(remainingWidthChars); // may run past end\n// after\nint dist = Math.Min(remainingWidthChars, breaker.PositionRemaining);\nif (dist > 0) breaker.MoveByDistance(dist);","handlingStrategy":"validation","validationCode":"int dist = Math.Clamp(distance, -position, containerText.Length - position);","typeGuard":"bool CanMoveBy(int position, int distance, int textLength) => position + distance >= 0 && position + distance <= textLength;","tryCatchPattern":"try { breaker.MoveByDistance(distance); }\ncatch (ArgumentException) { /* iteration past end: stop breaking */ break; }","preventionTips":["Clamp move distances to the remaining text length","Recreate the breaker when the underlying text changes mid-iteration","Track end-of-text explicitly rather than relying on the exception"],"tags":["wpf","wordbreaker","argument","out-of-bounds","text"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}