abpframework/abp · error · ArgumentException

len argument can not be bigger than given string's length!

Error message

len argument can not be bigger than given string's length!

What it means

Thrown by AbpStringExtensions.Left — the extension that takes the first len characters of a string. If len exceeds str.Length, no valid substring exists, so an ArgumentException is raised (after the null check via Check.NotNull). The same message text is shared with Right().

Source

Thrown at framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs:75

    /// </summary>
    [ContractAnnotation("str:null => true")]
    public static bool IsNullOrWhiteSpace([System.Diagnostics.CodeAnalysis.NotNullWhen(false)]this string? str)
    {
        return string.IsNullOrWhiteSpace(str);
    }

    /// <summary>
    /// Gets a substring of a string from beginning of the string.
    /// </summary>
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
    /// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
    public static string Left(this string str, int len)
    {
        Check.NotNull(str, nameof(str));

        if (str.Length < len)
        {
            throw new ArgumentException("len argument can not be bigger than given string's length!");
        }

        return str.Substring(0, len);
    }

    /// <summary>
    /// Converts line endings in the string to <see cref="Environment.NewLine"/>.
    /// </summary>
    public static string NormalizeLineEndings(this string str)
    {
        return str.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine);
    }

    /// <summary>
    /// Gets index of nth occurrence of a char in a string.
    /// </summary>
    /// <param name="str">source string to be searched</param>
    /// <param name="c">Char to search in <paramref name="str"/></param>

View on GitHub (pinned to 7ed43b1931)

Solutions

  1. Guard the length before calling: if (len > str.Length) len = str.Length; or use Math.Min.
  2. Validate input length upstream so the call site always receives a sufficiently long string.
  3. Prefer str.Length checks or take(0, Math.Min(len, str.Length)) semantics if truncation is acceptable.

Example fix

// before
var prefix = url.Left(10); // throws if url shorter than 10

// after
var prefix = url.Left(Math.Min(10, url.Length));
// or guard
if (url.Length < 10) { /* handle short case */ }
Defensive patterns

Strategy: validation

Validate before calling

// Clamp the length before calling Left
var safeLen = Math.Min(len, str.Length);
var prefix = str.Left(safeLen);

Prevention

When it happens

Trigger: Calling str.Left(len) where len > str.Length, e.g. "abc".Left(5). A negative len is handled by Substring's own ArgumentOutOfRangeException instead.

Common situations: Hard-coded length assumed larger than runtime input; off-by-one when computing a prefix length; calling Left on a possibly-empty string without checking length first.

Related errors


AI-assisted analysis of abpframework/abp@7ed43b1931 (2026-08-13). Data as JSON: /api/errors/204b6d1397597002. Report an issue: GitHub.