Skip to content

Commit

Permalink
SWEEP: Replace IndexOutOfRangeException with ArgumentOutOfRangeExcept…
Browse files Browse the repository at this point in the history
…ion except for indexer use, #1023 (#1083)
  • Loading branch information
paulirwin authored Jan 2, 2025
1 parent d95a40b commit dbc602c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 91 deletions.
4 changes: 2 additions & 2 deletions src/Lucene.Net.Benchmark/Support/Sax/Ext/Attributes2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface IAttributes2 : IAttributes
/// </summary>
/// <param name="index">The attribute index (zero-based).</param>
/// <returns>true if the attribute was declared in the DTD, false otherwise.</returns>
/// <exception cref="IndexOutOfRangeException">When the supplied index does not identify an attribute.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the supplied index does not identify an attribute.</exception>
bool IsDeclared(int index);

/// <summary>
Expand Down Expand Up @@ -79,7 +79,7 @@ public interface IAttributes2 : IAttributes
/// </summary>
/// <param name="index">The attribute index (zero-based).</param>
/// <returns>true if the value was found in the XML text, false if the value was provided by DTD defaulting.</returns>
/// <exception cref="IndexOutOfRangeException">When the supplied index does not identify an attribute.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the supplied index does not identify an attribute.</exception>
bool IsSpecified(int index);

/// <summary>
Expand Down
34 changes: 21 additions & 13 deletions src/Lucene.Net.Benchmark/Support/Sax/Ext/Attributes2Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ public Attributes2(IAttributes atts)
public bool IsDeclared(int index)
{
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(
"No attribute at index: " + index);
{
throw new ArgumentOutOfRangeException(nameof(index), $"No attribute at index: {index}");
}

return declared[index];
}

Expand Down Expand Up @@ -114,12 +116,14 @@ public bool IsDeclared(string qName)
/// </summary>
/// <param name="index">The attribute index (zero-based).</param>
/// <returns>current flag value</returns>
/// <exception cref="IndexOutOfRangeException">When the supplied index does not identify an attribute.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the supplied index does not identify an attribute.</exception>
public bool IsSpecified(int index)
{
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(
"No attribute at index: " + index);
{
throw new ArgumentOutOfRangeException(nameof(index), $"No attribute at index: {index}");
}

return specified[index];
}

Expand All @@ -128,7 +132,7 @@ public bool IsSpecified(int index)
/// </summary>
/// <param name="uri">The Namespace URI, or the empty string if the name has no Namespace URI.</param>
/// <param name="localName">The attribute's local name.</param>
/// <returns>current flag value</returns>
/// <returns>current flag value</returns>
/// <exception cref="ArgumentException">When the supplied names do not identify an attribute.</exception>
public bool IsSpecified(string uri, string localName)
{
Expand All @@ -146,7 +150,7 @@ public bool IsSpecified(string uri, string localName)
/// </summary>
/// <param name="qName">The XML qualified (prefixed) name.</param>
/// <returns>current flag value</returns>
/// <exception cref="ArgumentException">When the supplied name does not identify an attribute.</exception>
/// <exception cref="ArgumentException">When the supplied name does not identify an attribute.</exception>
public bool IsSpecified(string qName)
{
int index = GetIndex(qName);
Expand Down Expand Up @@ -253,12 +257,14 @@ public override void RemoveAttribute(int index)
/// </summary>
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="value">The desired flag value.</param>
/// <exception cref="IndexOutOfRangeException">When the supplied index does not identify an attribute.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the supplied index does not identify an attribute.</exception>
public virtual void SetDeclared(int index, bool value)
{
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(
"No attribute at index: " + index);
{
throw new ArgumentOutOfRangeException(nameof(index), $"No attribute at index: {index}");
}

declared[index] = value;
}

Expand All @@ -269,12 +275,14 @@ public virtual void SetDeclared(int index, bool value)
/// </summary>
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="value">The desired flag value.</param>
/// <exception cref="IndexOutOfRangeException">When the supplied index does not identify an attribute.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the supplied index does not identify an attribute.</exception>
public virtual void SetSpecified(int index, bool value)
{
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(
"No attribute at index: " + index);
{
throw new ArgumentOutOfRangeException(nameof(index), $"No attribute at index: {index}");
}

specified[index] = value;
}
}
Expand Down
42 changes: 21 additions & 21 deletions src/Lucene.Net.Benchmark/Support/Sax/Helpers/AttributesImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace Sax.Helpers
/// <item><description>to construct or modify an Attributes object in a SAX2 driver or filter.</description></item>
/// </list>
/// <para/>
/// This class replaces the now-deprecated SAX1 AttributeListImpl
/// This class replaces the now-deprecated SAX1 AttributeListImpl
/// class; in addition to supporting the updated Attributes
/// interface rather than the deprecated IAttributeList
/// interface rather than the deprecated IAttributeList
/// interface, it also includes a much more efficient
/// implementation using a single array rather than a set of Vectors.
/// </remarks>
Expand Down Expand Up @@ -322,7 +322,7 @@ public virtual string GetValue(string qName)
/// Clear the attribute list for reuse.
/// <para/>
/// Note that little memory is freed by this call:
/// the current array is kept so it can be
/// the current array is kept so it can be
/// reused.
/// </summary>
public virtual void Clear()
Expand Down Expand Up @@ -391,7 +391,7 @@ public virtual void AddAttribute(string uri, string localName, string qName,

/// <summary>
/// Set an attribute in the list.
///
///
/// <para/>For the sake of speed, this method does no checking
/// for name conflicts or well-formedness: such checks are the
/// responsibility of the application.
Expand All @@ -406,9 +406,9 @@ public virtual void AddAttribute(string uri, string localName, string qName,
/// if qualified names are not available.</param>
/// <param name="type">The attribute type as a string.</param>
/// <param name="value">The attribute value.</param>
/// <exception cref="IndexOutOfRangeException">When the
/// <exception cref="ArgumentOutOfRangeException">When the
/// supplied index does not point to an attribute
/// in the list.</exception>
/// in the list.</exception>
public virtual void SetAttribute(int index, string uri, string localName,
string qName, string type, string value)
{
Expand All @@ -430,7 +430,7 @@ public virtual void SetAttribute(int index, string uri, string localName,
/// Remove an attribute from the list.
/// </summary>
/// <param name="index">The index of the attribute (zero-based).</param>
/// <exception cref="IndexOutOfRangeException">When the supplied index does not point to an attribute in the list.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the supplied index does not point to an attribute in the list.</exception>
public virtual void RemoveAttribute(int index)
{
if (index >= 0 && index < length)
Expand Down Expand Up @@ -460,9 +460,9 @@ public virtual void RemoveAttribute(int index)
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="uri">The attribute's Namespace URI, or the empty
/// string for none.</param>
/// <exception cref="IndexOutOfRangeException">When the
/// <exception cref="ArgumentOutOfRangeException">When the
/// supplied index does not point to an attribute
/// in the list.</exception>
/// in the list.</exception>
public virtual void SetURI(int index, string uri)
{
if (index >= 0 && index < length)
Expand All @@ -481,9 +481,9 @@ public virtual void SetURI(int index, string uri)
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="localName">The attribute's local name, or the empty
/// string for none.</param>
/// <exception cref="IndexOutOfRangeException">When the
/// <exception cref="ArgumentOutOfRangeException">When the
/// supplied index does not point to an attribute
/// in the list.</exception>
/// in the list.</exception>
public virtual void SetLocalName(int index, string localName)
{
if (index >= 0 && index < length)
Expand All @@ -502,9 +502,9 @@ public virtual void SetLocalName(int index, string localName)
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="qName">The attribute's qualified name, or the empty
/// string for none.</param>
/// <exception cref="IndexOutOfRangeException">When the
/// <exception cref="ArgumentOutOfRangeException">When the
/// supplied index does not point to an attribute
/// in the list.</exception>
/// in the list.</exception>
public virtual void SetQName(int index, string qName)
{
if (index >= 0 && index < length)
Expand All @@ -522,9 +522,9 @@ public virtual void SetQName(int index, string qName)
/// </summary>
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="type">The attribute's type.</param>
/// <exception cref="IndexOutOfRangeException">When the
/// <exception cref="ArgumentOutOfRangeException">When the
/// supplied index does not point to an attribute
/// in the list.</exception>
/// in the list.</exception>
public virtual void SetType(int index, string type)
{
if (index >= 0 && index < length)
Expand All @@ -542,9 +542,9 @@ public virtual void SetType(int index, string type)
/// </summary>
/// <param name="index">The index of the attribute (zero-based).</param>
/// <param name="value">The attribute's value.</param>
/// <exception cref="IndexOutOfRangeException">When the
/// <exception cref="ArgumentOutOfRangeException">When the
/// supplied index does not point to an attribute
/// in the list.</exception>
/// in the list.</exception>
public virtual void SetValue(int index, string value)
{
if (index >= 0 && index < length)
Expand Down Expand Up @@ -601,12 +601,12 @@ private void EnsureCapacity(int n)
/// Report a bad array index in a manipulator.
/// </summary>
/// <param name="index">The index to report.</param>
/// <exception cref="IndexOutOfRangeException">Always.</exception>
/// <exception cref="ArgumentOutOfRangeException">Always.</exception>
[DoesNotReturn]
private static void BadIndex(int index) // LUCENENET: CA1822: Mark members as static
{
string msg =
"Attempt to modify attribute at illegal index: " + index;
throw new IndexOutOfRangeException(msg);
string msg = $"Attempt to modify attribute at illegal index: {index}";
throw new ArgumentOutOfRangeException(nameof(index), msg);
}


Expand Down
29 changes: 19 additions & 10 deletions src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ public override long Get(int docId)
try
{
if (docId < 0 || docId >= _outerInstance.maxDoc)
throw new IndexOutOfRangeException("docID must be 0 .. " + (_outerInstance.maxDoc - 1) +
"; got " + docId);
{
throw new ArgumentOutOfRangeException(nameof(docId),
$"docID must be 0 .. {_outerInstance.maxDoc - 1}; got {docId}");
}

_input.Seek(_field.DataStartFilePointer + (1 + _field.Pattern.Length + 2) * docId);
SimpleTextUtil.ReadLine(_input, _scratch);
Expand Down Expand Up @@ -273,8 +275,10 @@ public override void Get(int docId, BytesRef result)
try
{
if (docId < 0 || docId >= _outerInstance.maxDoc)
throw new IndexOutOfRangeException("docID must be 0 .. " + (_outerInstance.maxDoc - 1) +
"; got " + docId);
{
throw new ArgumentOutOfRangeException(nameof(docId),
$"docID must be 0 .. {_outerInstance.maxDoc - 1}; got {docId}");
}

_input.Seek(_field.DataStartFilePointer + (9 + _field.Pattern.Length + _field.MaxLength + 2) * docId);
SimpleTextUtil.ReadLine(_input, _scratch);
Expand Down Expand Up @@ -396,8 +400,8 @@ public override int GetOrd(int docId)
{
if (docId < 0 || docId >= _outerInstance.maxDoc)
{
throw new IndexOutOfRangeException("docID must be 0 .. " + (_outerInstance.maxDoc - 1) + "; got " +
docId);
throw new ArgumentOutOfRangeException(nameof(docId),
$"docID must be 0 .. {_outerInstance.maxDoc - 1}; got {docId}");
}

try
Expand Down Expand Up @@ -428,8 +432,10 @@ public override void LookupOrd(int ord, BytesRef result)
{
if (ord < 0 || ord >= _field.NumValues)
{
throw new IndexOutOfRangeException($"ord must be 0 .. {(_field.NumValues - 1)}; got {ord}");
throw new ArgumentOutOfRangeException(nameof(ord),
$"ord must be 0 .. {_field.NumValues - 1}; got {ord}");
}

_input.Seek(_field.DataStartFilePointer + ord * (9 + _field.Pattern.Length + _field.MaxLength));
SimpleTextUtil.ReadLine(_input, _scratch);
// LUCENENET specific - use wrapper BytesRefFormatter struct to defer building the string unless string.Format() is called
Expand Down Expand Up @@ -505,8 +511,10 @@ public override long NextOrd()
public override void SetDocument(int docID)
{
if (docID < 0 || docID >= _outerInstance.maxDoc)
throw new IndexOutOfRangeException("docID must be 0 .. " + (_outerInstance.maxDoc - 1) + "; got " +
docID);
{
throw new ArgumentOutOfRangeException(nameof(docID),
$"docID must be 0 .. {_outerInstance.maxDoc - 1}; got {docID}");
}

try
{
Expand All @@ -529,7 +537,8 @@ public override void LookupOrd(long ord, BytesRef result)
{
if (ord < 0 || ord >= _field.NumValues)
{
throw new IndexOutOfRangeException("ord must be 0 .. " + (_field.NumValues - 1) + "; got " + ord);
throw new ArgumentOutOfRangeException(nameof(ord),
$"ord must be 0 .. {_field.NumValues - 1}; got {ord}");
}

_input.Seek(_field.DataStartFilePointer + ord * (9 + _field.Pattern.Length + _field.MaxLength));
Expand Down
7 changes: 4 additions & 3 deletions src/Lucene.Net.Memory/MemoryIndexNormDocValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ namespace Lucene.Net.Index.Memory
* limitations under the License.
*/

///
///
/// <summary>
/// @lucene.internal
/// </summary>
internal class MemoryIndexNormDocValues : NumericDocValues
{
private readonly long value;

public MemoryIndexNormDocValues(long value)
{
this.value = value;
Expand All @@ -35,12 +36,12 @@ public override long Get(int docID)
{
if (docID != 0)
{
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(docID));
}
else
{
return value;
}
}
}
}
}
12 changes: 8 additions & 4 deletions src/Lucene.Net/Codecs/Lucene40/BitVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public void Set(int bit)
{
if (bit >= size)
{
throw new IndexOutOfRangeException("bit=" + bit + " size=" + size);
throw new ArgumentOutOfRangeException(nameof(bit), $"bit={bit} size={size}");
}

bits[bit >> 3] |= (byte)(1 << (bit & 7));
count = -1;
}
Expand All @@ -111,8 +112,9 @@ public bool GetAndSet(int bit)
{
if (bit >= size)
{
throw new IndexOutOfRangeException("bit=" + bit + " size=" + size);
throw new ArgumentOutOfRangeException(nameof(bit), $"bit={bit} size={size}");
}

int pos = bit >> 3;
int v = bits[pos];
int flag = 1 << (bit & 7);
Expand All @@ -139,8 +141,9 @@ public void Clear(int bit)
{
if (bit >= size)
{
throw new IndexOutOfRangeException(bit.ToString());
throw new ArgumentOutOfRangeException(nameof(bit), bit.ToString());
}

bits[bit >> 3] &= (byte)(~(1 << (bit & 7)));
count = -1;
}
Expand All @@ -149,8 +152,9 @@ public bool GetAndClear(int bit)
{
if (bit >= size)
{
throw new IndexOutOfRangeException(bit.ToString());
throw new ArgumentOutOfRangeException(nameof(bit), bit.ToString());
}

int pos = bit >> 3;
int v = bits[pos];
int flag = 1 << (bit & 7);
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Index/DocValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public override void SetDocument(int docID)

public override void LookupOrd(long ord, BytesRef result)
{
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(ord));
}

public override long ValueCount => 0;

public override long OrdAt(int index)
{
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index));
}

public override int Cardinality => 0;
Expand Down
Loading

0 comments on commit dbc602c

Please sign in to comment.