Skip to content

Commit

Permalink
SWEEP: Changed all applicable FormatException creation statements int…
Browse files Browse the repository at this point in the history
…o NumberFormatException.Create() (see #446)
  • Loading branch information
NightOwl888 committed Apr 26, 2021
1 parent f692ac5 commit af335ff
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Lucene.Net.Highlighter/Highlight/GradientFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static int HexToInt32(string hex)
{
int len = hex.Length;
if (len > 16)
throw ParseException.Create();
throw NumberFormatException.Create();

try
{
Expand All @@ -217,7 +217,7 @@ public static int HexToInt32(string hex)
}
catch (ArgumentException e)
{
throw ParseException.Create(e);
throw NumberFormatException.Create(e);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Lucene.Net/Support/ExceptionHandling/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ public static bool IsRuntimeException(this Exception e)

//if (e is null) return false;

//var typeofE = e.GetType();

//return e is IRuntimeException ||

// e is IndexOutOfRangeException ||
Expand All @@ -156,7 +154,7 @@ public static bool IsRuntimeException(this Exception e)

// e is SecurityException ||

// e is ObjectDisposedException ||
//// e is ObjectDisposedException || // AlreadyClosedException subclasses IOException, so this one doesn't count here

// // Known implemetnations of IRuntimeException
// //e is BytesRefHash.MaxBytesLengthExceededException ||
Expand All @@ -172,13 +170,13 @@ public static bool IsRuntimeException(this Exception e)

// e is InvalidCastException ||

// e is InvalidOperationException ||
// (e is InvalidOperationException && !(e is ObjectDisposedException)) ||

// e is MissingManifestResourceException ||

// typeof(FormatException).Equals(typeofE) || // Thrown only on datetime and number format problems, ignore all other FormatException errors (including ParseException)
// (e is FormatException && !(e is J2N.ParseException)) || // Thrown only on datetime and number format problems, ignore ParseException

// NUnitInconclusiveExceptionType is null ? false : NUnitInconclusiveExceptionType.IsAssignableFrom(typeofE);
// NUnitInconclusiveExceptionType is null ? false : NUnitInconclusiveExceptionType.IsAssignableFrom(e.GetType());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

namespace Lucene
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <summary>
/// The Java description is:
/// <para/>
/// Thrown to indicate that the application has attempted to convert a string to one of the numeric types,
/// but that the string does not have the appropriate format.
/// <para/>
/// This is a Java compatibility exception, and should be thrown in
/// Lucene.NET everywhere Lucene throws it, however catch blocks should
/// always use the <see cref="ExceptionExtensions.IsNumberFormatException(Exception)"/> method.
/// <code>
/// catch (Exception ex) when (ex.IsNumberFormatException())
/// </code>
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
#endif
internal class NumberFormatException : FormatException
{
[Obsolete("Use NumberFormatException.Create() instead.", error: true)]
public NumberFormatException()
{
}

[Obsolete("Use NumberFormatException.Create() instead.", error: true)]
public NumberFormatException(string message) : base(message)
{
}

[Obsolete("Use NumberFormatException.Create() instead.", error: true)]
public NumberFormatException(string message, Exception innerException) : base(message, innerException)
{
}

[Obsolete("Use NumberFormatException.Create() instead.", error: true)]
public NumberFormatException(Exception cause)
: base(cause?.ToString(), cause)
{
}

#if FEATURE_SERIALIZABLE_EXCEPTIONS
/// <summary>
/// Initializes a new instance of this class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected NumberFormatException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif

// Static factory methods

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create() => new FormatException();


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create(string message) => new FormatException(message);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create(string message, Exception innerException) => new FormatException(message, innerException);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create(Exception cause) => new FormatException(cause.Message, cause);
}
}
14 changes: 7 additions & 7 deletions src/Lucene.Net/Util/ArrayUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ public static int ParseInt32(char[] chars, int offset, int len, int radix)
int minRadix = 2, maxRadix = 36;
if (chars == null || radix < minRadix || radix > maxRadix)
{
throw new FormatException();
throw NumberFormatException.Create();
}
int i = 0;
if (len == 0)
{
throw new FormatException("chars length is 0");
throw NumberFormatException.Create("chars length is 0");
}
bool negative = chars[offset + i] == '-';
if (negative && ++i == len)
{
throw new FormatException("can't convert to an int");
throw NumberFormatException.Create("can't convert to an int");
}
if (negative == true)
{
Expand All @@ -124,16 +124,16 @@ private static int Parse(char[] chars, int offset, int len, int radix, bool nega
int digit = (int)char.GetNumericValue(chars[i + offset]);
if (digit == -1)
{
throw new FormatException("Unable to parse");
throw NumberFormatException.Create("Unable to parse");
}
if (max > result)
{
throw new FormatException("Unable to parse");
throw NumberFormatException.Create("Unable to parse");
}
int next = result * radix - digit;
if (next > result)
{
throw new FormatException("Unable to parse");
throw NumberFormatException.Create("Unable to parse");
}
result = next;
}
Expand All @@ -144,7 +144,7 @@ private static int Parse(char[] chars, int offset, int len, int radix, bool nega
result = -result;
if (result < 0)
{
throw new FormatException("Unable to parse");
throw NumberFormatException.Create("Unable to parse");
}
}
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/Lucene.Net/Util/NumericUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static int GetPrefixCodedInt64Shift(BytesRef val)
int shift = val.Bytes[val.Offset] - SHIFT_START_INT64;
if (shift > 63 || shift < 0)
{
throw new FormatException("Invalid shift value (" + shift + ") in prefixCoded bytes (is encoded value really an INT?)");
throw NumberFormatException.Create("Invalid shift value (" + shift + ") in prefixCoded bytes (is encoded value really an INT?)");
}
return shift;
}
Expand All @@ -238,7 +238,7 @@ public static int GetPrefixCodedInt32Shift(BytesRef val)
int shift = val.Bytes[val.Offset] - SHIFT_START_INT32;
if (shift > 31 || shift < 0)
{
throw new FormatException("Invalid shift value in prefixCoded bytes (is encoded value really an INT?)");
throw NumberFormatException.Create("Invalid shift value in prefixCoded bytes (is encoded value really an INT?)");
}
return shift;
}
Expand All @@ -262,7 +262,7 @@ public static long PrefixCodedToInt64(BytesRef val)
var b = val.Bytes[i];
if (b < 0)
{
throw new FormatException("Invalid prefixCoded numerical value representation (byte " + (b & 0xff).ToString("x") + " at position " + (i - val.Offset) + " is invalid)");
throw NumberFormatException.Create("Invalid prefixCoded numerical value representation (byte " + (b & 0xff).ToString("x") + " at position " + (i - val.Offset) + " is invalid)");
}
sortableBits |= (byte)b;
}
Expand All @@ -288,7 +288,7 @@ public static int PrefixCodedToInt32(BytesRef val)
var b = val.Bytes[i];
if (b < 0)
{
throw new FormatException("Invalid prefixCoded numerical value representation (byte " + (b & 0xff).ToString("x") + " at position " + (i - val.Offset) + " is invalid)");
throw NumberFormatException.Create("Invalid prefixCoded numerical value representation (byte " + (b & 0xff).ToString("x") + " at position " + (i - val.Offset) + " is invalid)");
}
sortableBits |= b;
}
Expand Down

0 comments on commit af335ff

Please sign in to comment.