diff --git a/src/Lucene.Net.Highlighter/Highlight/GradientFormatter.cs b/src/Lucene.Net.Highlighter/Highlight/GradientFormatter.cs index a015f3133d..ba8e60beb0 100644 --- a/src/Lucene.Net.Highlighter/Highlight/GradientFormatter.cs +++ b/src/Lucene.Net.Highlighter/Highlight/GradientFormatter.cs @@ -204,7 +204,7 @@ public static int HexToInt32(string hex) { int len = hex.Length; if (len > 16) - throw ParseException.Create(); + throw NumberFormatException.Create(); try { @@ -217,7 +217,7 @@ public static int HexToInt32(string hex) } catch (ArgumentException e) { - throw ParseException.Create(e); + throw NumberFormatException.Create(e); } } } diff --git a/src/Lucene.Net/Support/ExceptionHandling/ExceptionExtensions.cs b/src/Lucene.Net/Support/ExceptionHandling/ExceptionExtensions.cs index e6052b4833..8d943220fd 100644 --- a/src/Lucene.Net/Support/ExceptionHandling/ExceptionExtensions.cs +++ b/src/Lucene.Net/Support/ExceptionHandling/ExceptionExtensions.cs @@ -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 || @@ -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 || @@ -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()); } /// diff --git a/src/Lucene.Net/Support/ExceptionHandling/Exceptions/RuntimeExceptions/NumberFormatException.cs b/src/Lucene.Net/Support/ExceptionHandling/Exceptions/RuntimeExceptions/NumberFormatException.cs new file mode 100644 index 0000000000..aafd9cda8a --- /dev/null +++ b/src/Lucene.Net/Support/ExceptionHandling/Exceptions/RuntimeExceptions/NumberFormatException.cs @@ -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. + */ + + /// + /// The Java description is: + /// + /// 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. + /// + /// This is a Java compatibility exception, and should be thrown in + /// Lucene.NET everywhere Lucene throws it, however catch blocks should + /// always use the method. + /// + /// catch (Exception ex) when (ex.IsNumberFormatException()) + /// + /// + // 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 + /// + /// Initializes a new instance of this class with serialized data. + /// + /// The that holds the serialized object data about the exception being thrown. + /// The that contains contextual information about the source or destination. + 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); + } +} diff --git a/src/Lucene.Net/Util/ArrayUtil.cs b/src/Lucene.Net/Util/ArrayUtil.cs index 6ce42794e3..8e5b8bc2a4 100644 --- a/src/Lucene.Net/Util/ArrayUtil.cs +++ b/src/Lucene.Net/Util/ArrayUtil.cs @@ -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) { @@ -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; } @@ -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; diff --git a/src/Lucene.Net/Util/NumericUtils.cs b/src/Lucene.Net/Util/NumericUtils.cs index 3cc67cde02..269e7dbcd8 100644 --- a/src/Lucene.Net/Util/NumericUtils.cs +++ b/src/Lucene.Net/Util/NumericUtils.cs @@ -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; } @@ -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; } @@ -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; } @@ -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; }