JamesNK/Newtonsoft.Json · error · ArgumentOutOfRangeException
value
Error message
value
What it means
ArgumentOutOfRangeException(nameof(value)) from the CommentHandling property setter on JsonLoadSettings. The setter validates that the assigned enum is within the declared range (CommentHandling.Ignore..CommentHandling.Load); an out-of-range value (typically an uninitialised/cast integer) is rejected. nameof(value) is the formal parameter name, which is why the message is just "value".
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs:61
{
_lineInfoHandling = LineInfoHandling.Load;
_commentHandling = CommentHandling.Ignore;
_duplicatePropertyNameHandling = DuplicatePropertyNameHandling.Replace;
}
/// <summary>
/// Gets or sets how JSON comments are handled when loading JSON.
/// The default value is <see cref="CommentHandling.Ignore" />.
/// </summary>
/// <value>The JSON comment handling.</value>
public CommentHandling CommentHandling
{
get => _commentHandling;
set
{
if (value < CommentHandling.Ignore || value > CommentHandling.Load)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_commentHandling = value;
}
}
/// <summary>
/// Gets or sets how JSON line info is handled when loading JSON.
/// The default value is <see cref="LineInfoHandling.Load" />.
/// </summary>
/// <value>The JSON line info handling.</value>
public LineInfoHandling LineInfoHandling
{
get => _lineInfoHandling;
set
{
if (value < LineInfoHandling.Ignore || value > LineInfoHandling.Load)
{View on GitHub (pinned to 4f73e74372)
Solutions
- Validate the enum with Enum.IsDefined before assigning: if (Enum.IsDefined(typeof(CommentHandling), v)) settings.CommentHandling = v;
- Bind configuration using enum-aware binding that rejects unknown values upstream.
- Default-initialise JsonLoadSettings and only override validated values.
Example fix
// before
settings.CommentHandling = (CommentHandling)configValue;
// after
if (Enum.IsDefined(typeof(CommentHandling), configValue))
settings.CommentHandling = (CommentHandling)configValue; Defensive patterns
Strategy: validation
Validate before calling
if (Enum.IsDefined(typeof(CommentHandling), v)) settings.CommentHandling = (CommentHandling)v;
Type guard
static bool IsValidCommentHandling(int v) => Enum.IsDefined(typeof(CommentHandling), v);
Try / catch
try { settings.CommentHandling = (CommentHandling)configValue; } catch (ArgumentOutOfRangeException) { /* keep default */ } Prevention
- Validate enum values with Enum.IsDefined before assigning.
- Use enum-aware configuration binding.
- Default-initialise JsonLoadSettings and override only validated values.
When it happens
Trigger: Assigning settings.CommentHandling = (CommentHandling)999; or casting an arbitrary int to CommentHandling. Also when a config-binding system binds a numeric value that does not map to a defined enum member.
Common situations: Configuration-driven JsonLoadSettings where an invalid enum string/number is bound; reflection-based assignment of unknown enum ints; deserializing the settings object from JSON with an invalid enum value.
Related errors
- value
- Value must be positive.
- Value must be positive.
- Value must be positive.
- Indentation value must be greater than 0.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/2790dcdf302538ad.
Report an issue: GitHub.