bchavez/Bogus · error · ArgumentException
There are no values after exclusion to choose from.
Error message
There are no values after exclusion to choose from.
What it means
Thrown by Randomizer.Enum<T> when, after applying the exclude list, no enum names remain in the selection. The method computes selection = Enum.GetNames(T).Except(exclude) and rejects an empty result because there is nothing to pick.
Source
Thrown at Source/Bogus/Randomizer.cs:671
/// <typeparam name="T">Must be an Enum</typeparam>
/// <param name="exclude">Exclude enum values from being returned</param>
public T Enum<T>(params T[] exclude) where T : struct, Enum
{
var e = typeof(T);
if( !e.IsEnum() )
throw new ArgumentException("When calling Enum<T>() with no parameters T must be an enum.");
var selection = System.Enum.GetNames(e);
if( exclude.Any() )
{
var excluded = exclude.Select(ex => System.Enum.GetName(e, ex));
selection = selection.Except(excluded).ToArray();
}
if( !selection.Any() )
{
throw new ArgumentException("There are no values after exclusion to choose from.");
}
var val = this.ArrayElement(selection);
System.Enum.TryParse(val, out T picked);
return picked;
}
/// <summary>
/// Picks a random subset of enum values in T:Enum.
/// </summary>
/// <typeparam name="T">The enum.</typeparam>
/// <param name="count">The number of enums to pick.</param>
/// <param name="exclude">Any enums that should be excluded before picking.</param>
public T[] EnumValues<T>(int? count = null, params T[] exclude) where T : Enum
{
T[] enums;
if( exclude.Length > 0)View on GitHub (pinned to 6ece18c5c2)
Solutions
- Ensure at least one enum member is not in the exclude list.
- Validate before calling: var remaining = Enum.GetValues<T>().Except(exclude); require remaining.Any().
- Reconsider the exclude set if it matches all members.
Example fix
// before
var v = f.Random.Enum(Enum.GetValues<MyEnum>().ToArray());
// after
var pool = Enum.GetValues<MyEnum>().Except(new[]{MyEnum.None}).ToArray();
var v = pool.Length > 0 ? f.Random.Enum(pool) : MyEnum.None; Defensive patterns
Strategy: validation
Validate before calling
static T SafeEnum<T>(Bogus.Randomizer r, params T[] exclude) where T : struct, Enum {
var remaining = System.Enum.GetValues<T>().Except(exclude).ToArray();
if (remaining.Length == 0) throw new InvalidOperationException("all enum values excluded");
return r.Enum(exclude);
} Type guard
bool HasRemaining<T>(params T[] exclude) where T : struct, Enum => System.Enum.GetValues<T>().Except(exclude).Any();
Try / catch
try { return r.Enum(exclude); }
catch (ArgumentException ex) when (ex.Message.Contains("no values after exclusion")) {
return System.Enum.GetValues<T>()[0]; // default first member
} Prevention
- Ensure exclude is a strict subset of enum members.
- Validate remaining pool before calling.
- Avoid excluding all members of small enums.
When it happens
Trigger: Calling f.Random.Enum(exclude: everyValue) i.e. excluding all members of the enum, or excluding the sole member of a single-value enum.
Common situations: Building exclude lists dynamically and accidentally covering the whole enum; enums with one value where the caller excludes it.
Related errors
- When calling Enum<T>() with no parameters T must be an enum.
- max digit can't be lager than 9 or smaller than 0
- The min/max range is invalid. The minimum value '{min}' is g
- The specified range does not contain any even numbers.
- The specified range does not contain any odd numbers.
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/5adcaced7916730b.
Report an issue: GitHub.