dotnet/machinelearning · error · ArgumentException

InvalidFieldWidths

Error message

InvalidFieldWidths

What it means

TextFieldParser.SetFieldWidths validates that every fixed-width entry is at least 1 and throws ArgumentException with InvalidFieldWidths when any width is zero or negative. A field width of 0 or less makes line slicing impossible/meaningless, so the library rejects the widths configuration up front.

Source

Thrown at src/Microsoft.Data.Analysis/TextFieldParser.cs:978

                    Debug.Assert(_fieldWidths[i] > 0, "Bad field width, this should have been caught on input");
                    _lineLength += _fieldWidths[i];
                }
                if (_fieldWidths[widthBound] > 0)
                {
                    _lineLength += _fieldWidths[widthBound];
                }
            }
        }

        private void ValidateFieldWidthsOnInput(int[] widths)
        {
            Debug.Assert(widths != null, "There are no field widths");
            int bound = widths.Length - 1;
            for (int i = 0; i <= bound - 1; i++)
            {
                if (widths[i] < 1)
                {
                    throw new ArgumentException(Strings.InvalidFieldWidths);
                }
            }
        }

        private void ValidateAndEscapeDelimiters()
        {
            if (_delimiters == null)
            {
                throw new Exception(Strings.NullDelimiters);
            }
            if (_delimiters.Length == 0)
            {
                throw new Exception(Strings.EmptyDelimiters);
            }
            int length = _delimiters.Length;
            StringBuilder builder = new StringBuilder();
            StringBuilder quoteBuilder = new StringBuilder();
            quoteBuilder.Append(EndQuotePattern + "(");

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Fix the widths array so every entry is >= 1.
  2. Validate each width before calling SetFieldWidths and surface which entry is invalid.
  3. If a column can be variable/optional, model it as a delimiter or handle it in code rather than a zero width.
  4. Check width computation math (start/end offsets) for the off-by-one or sign error.

Example fix

// before
parser.SetFieldWidths(new int[] { 10, 0, 15 }); // throws InvalidFieldWidths
// after
int[] widths = new int[] { 10, 5, 15 };
if (widths.Any(w => w < 1))
    throw new ArgumentException("All field widths must be positive integers.");
parser.SetFieldWidths(widths);
Defensive patterns

Strategy: validation

Validate before calling

if (widths == null || widths.Any(w => w < 1))
    throw new ArgumentException("All field widths must be >= 1.");
parser.SetFieldWidths(widths);

Type guard

static bool AllWidthsPositive(int[] widths) => widths != null && widths.All(w => w >= 1);

Try / catch

try
{
    parser.SetFieldWidths(widths);
}
catch (ArgumentException ex)
{
    logger.LogError(ex, "Invalid field widths: {Widths}", string.Join(",", widths ?? Array.Empty<int>()));
    throw;
}

Prevention

When it happens

Trigger: Calling SetFieldWidths with an array containing 0 or a negative number, e.g. SetFieldWidths(new int[] { 5, 0, 10 }) or SetFieldWidths(new int[] { -1, 8 }), then reading fields.

Common situations: Computing widths from parsed format specs where a missing value defaults to 0; off-by-one arithmetic producing 0; subtracting offsets in the wrong order yielding negative widths; user-supplied column layout containing a blank entry.

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


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/d71a72df97ca433d. Report an issue: GitHub.