Skip to content

Commit

Permalink
SWEEP: Reviewed all catch blocks where Lucene uses IndexOutOfBoundExc…
Browse files Browse the repository at this point in the history
…eption and converted them to use our IsIndexOutOfBoundsException() extension method (see apache#446).
  • Loading branch information
NightOwl888 committed Apr 14, 2021
1 parent 7a9f7fc commit 8714ade
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ private string GetAliasValue(int id)
{
return aliases[id - 1];
}
catch (IndexOutOfRangeException ex)
catch (Exception ex) when (ex.IsIndexOutOfBoundsException())
{
throw new ArgumentException("Bad flag alias number:" + id, ex);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using J2N.IO;
using Lucene;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -142,7 +143,7 @@ public override string GetFully(string key)
}
}
}
catch (ArgumentOutOfRangeException /*x*/) { }
catch (Exception x) when (x.IsIndexOutOfBoundsException()) { }
return result.ToString();
}

Expand Down Expand Up @@ -200,7 +201,7 @@ public override string GetLastOnPath(string key)
}
}
}
catch (ArgumentOutOfRangeException /*x*/) { }
catch (Exception x) when (x.IsIndexOutOfBoundsException()) { }
return result.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public virtual void TestCodePointAtCharSequenceInt()
java4.CodePointAt(highSurrogateAt3, 4);
fail("string index out of bounds");
}
catch (ArgumentOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
}

Expand All @@ -60,7 +60,7 @@ public virtual void TestCodePointAtCharSequenceInt()
java5.CodePointAt(highSurrogateAt3, 4);
fail("string index out of bounds");
}
catch (ArgumentOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public virtual void TestOffsetByCodePoint()
var to = java4.OffsetByCodePoints(s, 0, s.Length, index, offset);
assertEquals(to, index + offset);
}
catch (ArgumentOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
assertTrue((index + offset) < 0 || (index + offset) > s.Length);
}
Expand All @@ -120,14 +120,14 @@ public virtual void TestOffsetByCodePoint()
{
o = java5.OffsetByCodePoints(s, 0, s.Length, index, offset);
}
catch (ArgumentOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
try
{
Character.OffsetByCodePoints(s, 0, s.Length, index, offset);
fail();
}
catch (ArgumentOutOfRangeException)
catch (Exception e2) when (e2.IsIndexOutOfBoundsException())
{
// OK
}
Expand Down
16 changes: 4 additions & 12 deletions src/Lucene.Net.Tests/Index/TestSegmentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ public virtual void TestOutOfBoundsAccess()
reader.Document(-1);
Assert.Fail();
}
#pragma warning disable 168
catch (IndexOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
}

Expand All @@ -246,9 +244,7 @@ public virtual void TestOutOfBoundsAccess()
reader.GetTermVectors(-1);
Assert.Fail();
}
#pragma warning disable 168
catch (IndexOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
}

Expand All @@ -257,9 +253,7 @@ public virtual void TestOutOfBoundsAccess()
reader.Document(numDocs);
Assert.Fail();
}
#pragma warning disable 168
catch (IndexOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
}

Expand All @@ -268,9 +262,7 @@ public virtual void TestOutOfBoundsAccess()
reader.GetTermVectors(numDocs);
Assert.Fail();
}
#pragma warning disable 168
catch (IndexOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
}
}
Expand Down
30 changes: 9 additions & 21 deletions src/Lucene.Net.Tests/Util/TestCharsRef.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using J2N.Text;
using J2N.Text;
using Lucene.Net.Attributes;
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -133,7 +133,7 @@ public virtual void TestCopyCharsRef()
// c.CharAt(-1);
// Assert.Fail();
// }
// catch (IndexOutOfRangeException expected)
// catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
// {
// // expected exception
// }
Expand All @@ -143,7 +143,7 @@ public virtual void TestCopyCharsRef()
// c.CharAt(3);
// Assert.Fail();
// }
// catch (IndexOutOfRangeException expected)
// catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
// {
// // expected exception
// }
Expand All @@ -162,9 +162,7 @@ public virtual void TestCharSequenceIndexer()
var _ = c[-1];
Assert.Fail();
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
// expected exception
}
Expand All @@ -174,9 +172,7 @@ public virtual void TestCharSequenceIndexer()
var _ = c[3];
Assert.Fail();
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
// expected exception
}
Expand Down Expand Up @@ -216,9 +212,7 @@ private void DoTestSequence(ICharSequence c)
c.Subsequence(-1, 1 - -1); // LUCENENET: Corrected 2nd parameter
Assert.Fail();
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
// expected exception
}
Expand All @@ -228,9 +222,7 @@ private void DoTestSequence(ICharSequence c)
c.Subsequence(0, -1 - 0); // LUCENENET: Corrected 2nd parameter
Assert.Fail();
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
// expected exception
}
Expand All @@ -240,9 +232,7 @@ private void DoTestSequence(ICharSequence c)
c.Subsequence(0, 4 - 0); // LUCENENET: Corrected 2nd parameter
Assert.Fail();
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
// expected exception
}
Expand All @@ -252,9 +242,7 @@ private void DoTestSequence(ICharSequence c)
c.Subsequence(2, 1 - 2); // LUCENENET: Corrected 2nd parameter
Assert.Fail();
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException expected)
#pragma warning restore 168
catch (Exception expected) when (expected.IsIndexOutOfBoundsException())
{
// expected exception
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Tests/Util/TestPriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public static void TestOverflow()
pq.Add(3);
Assert.Fail();
}
catch (IndexOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
}

Expand All @@ -434,7 +434,7 @@ public static void TestOverflow()
{
pq.Add(666);
}
catch (IndexOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ public virtual void TestNewString()
Assert.AreEqual(cpString.Substring(rs, rc), str);
continue;
}
#pragma warning disable 168
catch (ArgumentOutOfRangeException e1) // LUCENENET specific - changed from IndexOutOfBoundsException to ArgumentOutOfRangeException (.NET convention)
#pragma warning restore 168
catch (Exception e1) when (e1.IsIndexOutOfBoundsException())
{
// Ignored.
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Util/BytesRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public override string ToString()
{
return bytesRef.Utf8ToString();
}
catch (IndexOutOfRangeException)
catch (Exception e) when (e.IsIndexOutOfBoundsException())
{
return bytesRef.ToString();
}
Expand Down

0 comments on commit 8714ade

Please sign in to comment.