dotnet/wpf · error · ArgumentException
SR.BadDistance
Error message
SR.BadDistance
What it means
DocumentSequenceTextPointer.CreatePointer throws ArgumentException (BadDistance) when the requested offset distance cannot be honored by scanning the text container. xGapAwareScan returns false when the distance overshoots the content or cannot be achieved respecting gaps between child pages, so the call fails rather than returning a pointer at an undefined position.
Solutions
- Clamp distance to the container's symbol count (e.g. via the symbol offsets of the start/end pointers) before calling.
- Use a smaller step size and iterate moves, checking position between steps.
- Catch ArgumentException and fall back to a CreatePointer() at the current position.
Example fix
// before var p = docPointer.CreatePointer(bigDistance); // after var distance = Math.Min(bigDistance, Math.Abs(endOffset - currentOffset)); var p = docPointer.CreatePointer(distance);
Defensive patterns
Strategy: validation
Validate before calling
var max = Math.Abs(docPointer.TextContainerEndOffset - docPointer.Offset); distance = Math.Clamp(requested, -max, max);
Try / catch
try { p = pointer.CreatePointer(distance); } catch (ArgumentException) { p = pointer.CreatePointer(); /* stay at current position */ } Prevention
- Clamp distance against the container's symbol range before calling.
- Advance large distances in bounded steps.
- Don't reuse distances computed for a different text container.
When it happens
Trigger: Calling CreatePointer(distance) on a DocumentSequenceTextPointer with a distance larger than the remaining symbol count (or smaller than the available backward distance) in the sequence — the virtualized multi-page container cannot place a pointer that far away.
Common situations: Advancing a pointer by a fixed stride without checking remaining document length; distances computed from positions in a different text container; documents with gap (page-boundary) regions that make nominal distances unrecoverable.
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.
Related errors
- SR.BadTextPositionOrder
- SR.Format(SR.MaxLengthExceedsBufferSize, count…
- SR.Format(SR.NegativeValue, "count")
- SR.Format(SR.NegativeValue, "startIndex")
- SR.Format(SR.StartIndexExceedsBufferSize, startIndex…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/fa8cbe56ec9d41e4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs:785
}
/// <summary>
/// <see cref="ITextPointer.CreatePointer(int,LogicalDirection)"/>
/// </summary>
public static ITextPointer CreatePointer(DocumentSequenceTextPointer thisTp, int distance, LogicalDirection gravity)
{
ValidationHelper.VerifyDirection(gravity, "gravity");
// Special case for common case of distance == 0
// to avoid calculating child container size, which
// could be expansive especially in case where child
// container requires virtualization.
DocumentSequenceTextPointer newTp = new DocumentSequenceTextPointer(thisTp.ChildBlock, thisTp.ChildPointer.CreatePointer(gravity));
if (distance != 0)
{
if (!xGapAwareScan(newTp, distance))
{
throw new ArgumentException(SR.BadDistance, nameof(distance));
}
}
return newTp;
}
#endregion TextPointer Methods
//------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------
//------------------------------------------------------
//
// Public Events
//View on GitHub (pinned to 81131a70a4)