TheAlgorithms/C-Sharp · error · ArgumentException

Upper bound must be greater than 0.

Error message

Upper bound must be greater than 0.

What it means

GetAutomorphicNumbers throws ArgumentException when upperBound < 1, since the range must end at a positive number. This is the second bound check, after the lowerBound check and before the lowerBound <= upperBound check.

Solutions

  1. Pass upperBound >= 1; if you need 'no limit', pick a large positive constant instead of 0.
  2. Clamp: upperBound = Math.Max(1, upperBound) or skip the call entirely when the requested range is empty.
  3. Validate bounds together: require 1 <= lowerBound <= upperBound before calling.

Example fix

// before
var nums = AutomorphicNumber.GetAutomorphicNumbers(1, upper); // upper may be 0
// after
var nums = upper >= 1 ? AutomorphicNumber.GetAutomorphicNumbers(1, upper) : Enumerable.Empty<int>();
Defensive patterns

Strategy: validation

Validate before calling

if (upperBound < 1) return Enumerable.Empty<int>(); // empty requested range, skip call
if (lowerBound < 1 || lowerBound > upperBound) throw new ArgumentOutOfRangeException(nameof(lowerBound));

Type guard

static bool IsValidRange(int lo, int hi) => lo >= 1 && hi >= 1 && lo <= hi;

Try / catch

try { seq = AutomorphicNumber.GetAutomorphicNumbers(lo, hi); }
catch (ArgumentException ex) when (ex.Message.Contains("Upper bound")) { /* treat as empty range or fix hi */ }

Prevention

When it happens

Trigger: Calling GetAutomorphicNumbers(lowerBound, upperBound) with upperBound of 0 or negative, e.g. GetAutomorphicNumbers(1, 0).

Common situations: Empty range requests encoded as (1, 0) instead of an early return; upper limits computed from data that can be zero/negative; config defaults of 0 for 'no limit'.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/58ebea883d67cdcf. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/AutomorphicNumber.cs:28

    /// Generates a list of automorphic numbers that are between <paramref name="lowerBound"/> and <paramref name="upperBound"/>
    /// inclusive.
    /// </summary>
    /// <param name="lowerBound">The lower bound of the list.</param>
    /// <param name="upperBound">The upper bound of the list.</param>
    /// <returns>A list that contains all of the automorphic numbers between <paramref name="lowerBound"/> and <paramref name="upperBound"/> inclusive.</returns>
    /// <exception cref="ArgumentException">If the <paramref name="lowerBound"/>
    /// or <paramref name="upperBound"/> is not greater than zero
    /// or <paramref name="upperBound"/>is lower than the <paramref name="lowerBound"/>.</exception>
    public static IEnumerable<int> GetAutomorphicNumbers(int lowerBound, int upperBound)
    {
        if (lowerBound < 1)
        {
            throw new ArgumentException($"Lower bound must be greater than 0.");
        }

        if (upperBound < 1)
        {
            throw new ArgumentException($"Upper bound must be greater than 0.");
        }

        if (lowerBound > upperBound)
        {
            throw new ArgumentException($"The lower bound must be less than or equal to the upper bound.");
        }

        return Enumerable.Range(lowerBound, upperBound).Where(IsAutomorphic);
    }

    /// <summary>
    /// Checks if a given natural number is automorphic or not.
    /// </summary>
    /// <param name="number">The number to check.</param>
    /// <returns>True if the number is automorphic, false otherwise.</returns>
    /// <exception cref="ArgumentException">If the number is non-positive.</exception>
    public static bool IsAutomorphic(int number)
    {

View on GitHub (pinned to 96e2905cab)