New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Double(Single).Parse(string) and Double(Single).Parse(ReadOnlySpan<char) throws different exception for null #76442
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Well, According to the doc, making existing code throw less exceptions is in "Bucket 2: Reasonable Grey Area", which sounds like it could be acceptable if the impact is deemed low enough. |
If this is normal - how should migration looks like in dotnet repo ? |
That said, probably any of our callers should be checking/enforcing against null args anyways, if they then delegate to a Span implementation. For clarity reasons if nothing else. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue Details
Originally posted by @krwq in #75452 (comment) This makes migration from string to ReadOnlySpan a bit tricky. //Parse with null string test = null;
ReadOnlySpan<char> ros = test.AsSpan().Trim();
try
{
double dVal = double.Parse(ros, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
}
catch (FormatException){}
try
{
double dVal = double.Parse(test, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
}
catch (ArgumentNullException) { }
//Parse with empty
string testEmpty = "";
ReadOnlySpan<char> rosEmpty = testEmpty.AsSpan().Trim();
try
{
double dVal = double.Parse(rosEmpty, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
}
catch (FormatException) { }
try
{
double dVal = double.Parse(testEmpty, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
}
catch (FormatException) { }
|
This difference is by design and has been this way for since .NET Core 2.1 when As indicated, In general Could you please clarify why you need to catch the exception here, let alone the exact exception? Most exception types are indicative of errors and shouldn't be caught in regular circumstances (such as for control flow). In the case of something like the |
This issue has been marked |
@tannergooding Thank you for the detailed explanation. I also think in this direction, just wanted to be sure that will not introduce any breaking change when replacing Double(Single).Parse with their counter Span/ROSpan version. |
Originally posted by @krwq in #75452 (comment)
This makes migration from string to ReadOnlySpan a bit tricky.
Repro:
//Parse with null
The text was updated successfully, but these errors were encountered: