From 21f47593df35f30f75ece4ebf3c4f0c6083746a2 Mon Sep 17 00:00:00 2001 From: neutmute Date: Tue, 16 Feb 2016 22:15:14 +1100 Subject: [PATCH 1/2] #132 Handle ambiguous abbreviation parsing by throwing exception --- UnitsNet.Tests/UnitSystemTests.cs | 38 ++++++++++- UnitsNet/CustomCode/UnitSystem.cs | 67 ++++++++++++++----- .../GeneratedCode/UnitSystem.Default.g.cs | 2 +- UnitsNet/Scripts/UnitDefinitions/Volume.json | 2 +- UnitsNet/UnitsNetException.cs | 11 +++ 5 files changed, 99 insertions(+), 21 deletions(-) diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index d73bef5ce9..1ced1c221d 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -71,6 +71,13 @@ private enum CustomUnit // ReSharper restore UnusedMember.Local } + private UnitSystem GetCachedUnitSystem() + { + var cultureInfo = CultureInfo.GetCultureInfo("en-US"); + var unitSystem = UnitSystem.GetCached(cultureInfo); + return unitSystem; + } + private static IEnumerable GetUnitTypesWithMissingAbbreviations(string cultureName, IEnumerable unitValues) where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable @@ -161,12 +168,39 @@ public void DecimalPointDigitGroupingCultureFormatting(string culture) Assert.AreEqual("1.111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, new CultureInfo(culture))); } + [Test] + public void Parse_UnambiguousUnitsDoesNotThrow() + { + var unitSystem = GetCachedUnitSystem(); + var unit = Volume.Parse("1 l"); + + Assert.AreEqual(Volume.FromLiters(1), unit); + } + + [Test] + public void Parse_AmbiguousUnitsThrowsException() + { + var unitSystem = GetCachedUnitSystem(); + + // Act 1 + Assert.Throws( ()=>unitSystem.Parse("tsp")); + + // Act 2 + try + { + Volume.Parse("1 tsp"); + } + catch (UnitsNetException e) + { + Assert.True(e.InnerException is AmbiguousUnitParseException); + } + } + [TestCase("m^2", Result = AreaUnit.SquareMeter)] [TestCase("cm^2", Result = AreaUnit.Undefined)] public AreaUnit Parse_ReturnsUnitMappedByCustomAbbreviationOrUndefined(string unitAbbreviationToParse) { - CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-US"); - UnitSystem unitSystem = UnitSystem.GetCached(cultureInfo); + var unitSystem = GetCachedUnitSystem(); unitSystem.MapUnitToAbbreviation(AreaUnit.SquareMeter, "m^2"); return unitSystem.Parse(unitAbbreviationToParse); diff --git a/UnitsNet/CustomCode/UnitSystem.cs b/UnitsNet/CustomCode/UnitSystem.cs index 433a01cb6e..fb5fd596bc 100644 --- a/UnitsNet/CustomCode/UnitSystem.cs +++ b/UnitsNet/CustomCode/UnitSystem.cs @@ -28,8 +28,15 @@ // ReSharper disable once CheckNamespace + + namespace UnitsNet { + public class AbbreviationMap : Dictionary> + { + + } + [PublicAPI] public partial class UnitSystem { @@ -41,7 +48,7 @@ public partial class UnitSystem /// Per-unit-type dictionary of enum values by abbreviation. This is the inverse of /// . /// - private readonly Dictionary> _unitTypeToAbbrevToUnitValue; + private readonly Dictionary _unitTypeToAbbrevToUnitValue; /// /// Per-unit-type dictionary of abbreviations by enum value. This is the inverse of @@ -71,7 +78,7 @@ public UnitSystem([CanBeNull] IFormatProvider cultureInfo = null) Culture = cultureInfo; _unitTypeToUnitValueToAbbrevs = new Dictionary>>(); - _unitTypeToAbbrevToUnitValue = new Dictionary>(); + _unitTypeToAbbrevToUnitValue = new Dictionary(); LoadDefaultAbbreviatons(cultureInfo); } @@ -122,17 +129,33 @@ public TUnit Parse(string unitAbbreviation) where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable { Type unitType = typeof (TUnit); - Dictionary abbrevToUnitValue; + AbbreviationMap abbrevToUnitValue; if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue)) throw new NotImplementedException( $"No abbreviations defined for unit type [{unitType}] for culture [{Culture}]."); - int unitValue; - TUnit result = abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValue) - ? (TUnit) (object) unitValue - : default(TUnit); + List unitValues; + List units; + + if (abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValues)) + { + units = unitValues.Cast().Distinct().ToList(); + } + else + { + units = new List(); + } - return result; + switch (units.Count) + { + case 1: + return units[0]; + case 0: + return default(TUnit); + default: + var unitsCsv = String.Join(", ", units.Select(x => x.ToString()).ToArray()); + throw new AmbiguousUnitParseException($"Cannot parse '{unitAbbreviation}' since it could be either of these: {unitsCsv}"); + } } [PublicAPI] @@ -187,15 +210,17 @@ public void MapUnitToAbbreviation(Type unitType, int unitValue, [NotNull] params unitValueToAbbrev[unitValue] = existingAbbreviations.Concat(abbreviations).Distinct().ToList(); foreach (string abbreviation in abbreviations) { - Dictionary abbrevToUnitValue; + AbbreviationMap abbrevToUnitValue; if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue)) { - abbrevToUnitValue = _unitTypeToAbbrevToUnitValue[unitType] = - new Dictionary(); + abbrevToUnitValue = _unitTypeToAbbrevToUnitValue[unitType] = new AbbreviationMap(); } if (!abbrevToUnitValue.ContainsKey(abbreviation)) - abbrevToUnitValue[abbreviation] = unitValue; + { + abbrevToUnitValue[abbreviation] = new List(); + } + abbrevToUnitValue[abbreviation].Add(unitValue); } } @@ -205,11 +230,11 @@ public bool TryParse(string unitAbbreviation, out TUnit unit) { Type unitType = typeof (TUnit); - Dictionary abbrevToUnitValue; - int unitValue; + AbbreviationMap abbrevToUnitValue; + List unitValues; if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue) || - !abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValue)) + !abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValues)) { if (IsDefaultCulture) { @@ -221,8 +246,16 @@ public bool TryParse(string unitAbbreviation, out TUnit unit) return GetCached(DefaultCulture).TryParse(unitAbbreviation, out unit); } - unit = (TUnit) (object) unitValue; - return true; + var maps = (List) (object) unitValues; + + switch (maps.Count) + { + case 1: unit = maps[0]; + return true; + default: + unit = default(TUnit); + return false; + } } /// diff --git a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs index 60a4a68db5..52c32909d3 100644 --- a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs +++ b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs @@ -1980,7 +1980,7 @@ private static readonly ReadOnlyCollection DefaultLocalization new CulturesForEnumValue((int) VolumeUnit.MetricTeaspoon, new[] { - new AbbreviationsForCulture("en-US", ""), + new AbbreviationsForCulture("en-US", "tsp", "t", "ts", "tspn", "t.", "ts.", "tsp.", "tspn.", "teaspoon"), new AbbreviationsForCulture("ru-RU", ""), new AbbreviationsForCulture("nb-NO", ""), }), diff --git a/UnitsNet/Scripts/UnitDefinitions/Volume.json b/UnitsNet/Scripts/UnitDefinitions/Volume.json index f1c0c58393..97428ade43 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Volume.json +++ b/UnitsNet/Scripts/UnitDefinitions/Volume.json @@ -359,7 +359,7 @@ "Localization": [ { "Culture": "en-US", - "Abbreviations": [] + "Abbreviations": ["tsp","t", "ts", "tspn", "t.", "ts.", "tsp.", "tspn.", "teaspoon"] }, { "Culture": "ru-RU", diff --git a/UnitsNet/UnitsNetException.cs b/UnitsNet/UnitsNetException.cs index ec344ee054..3c57360991 100644 --- a/UnitsNet/UnitsNetException.cs +++ b/UnitsNet/UnitsNetException.cs @@ -37,4 +37,15 @@ public UnitsNetException(string message, Exception innerException) : base(messag { } } + + public class AmbiguousUnitParseException : UnitsNetException + { + public AmbiguousUnitParseException(string message) : base(message) + { + } + + public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException) + { + } + } } \ No newline at end of file From 0f1d0963d0e6ab7213b8106cfc068f240d245243 Mon Sep 17 00:00:00 2001 From: neutmute Date: Wed, 17 Feb 2016 09:16:03 +1100 Subject: [PATCH 2/2] #132 ambiguous unit handling - ensure throw appropriate exception + tidyup --- UnitsNet.Tests/UnitSystemTests.cs | 9 +---- UnitsNet/AmbiguousUnitParseException.cs | 36 +++++++++++++++++++ UnitsNet/CustomCode/UnitSystem.cs | 13 ++++--- .../UnitClasses/Acceleration.g.cs | 6 +++- .../UnitClasses/AmplitudeRatio.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Area.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Density.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Duration.g.cs | 6 +++- .../UnitClasses/ElectricCurrent.g.cs | 6 +++- .../UnitClasses/ElectricPotential.g.cs | 6 +++- .../UnitClasses/ElectricResistance.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Energy.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Force.g.cs | 6 +++- .../UnitClasses/ForceChangeRate.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Frequency.g.cs | 6 +++- .../UnitClasses/Information.g.cs | 6 +++- .../UnitClasses/KinematicViscosity.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Length.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Level.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs | 6 +++- .../GeneratedCode/UnitClasses/MassFlow.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Power.g.cs | 6 +++- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Pressure.g.cs | 6 +++- .../UnitClasses/PressureChangeRate.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs | 6 +++- .../UnitClasses/RotationalSpeed.g.cs | 6 +++- .../UnitClasses/SpecificEnergy.g.cs | 6 +++- .../UnitClasses/SpecificWeight.g.cs | 6 +++- UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs | 6 +++- .../UnitClasses/Temperature.g.cs | 6 +++- .../UnitClasses/TemperatureChangeRate.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Torque.g.cs | 6 +++- .../GeneratedCode/UnitClasses/VitaminA.g.cs | 6 +++- .../GeneratedCode/UnitClasses/Volume.g.cs | 6 +++- .../Include-GenerateUnitClassSourceCode.ps1 | 6 +++- UnitsNet/UnitsNetException.cs | 11 ------ 39 files changed, 220 insertions(+), 59 deletions(-) create mode 100644 UnitsNet/AmbiguousUnitParseException.cs diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index 1ced1c221d..9cd6266668 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -186,14 +186,7 @@ public void Parse_AmbiguousUnitsThrowsException() Assert.Throws( ()=>unitSystem.Parse("tsp")); // Act 2 - try - { - Volume.Parse("1 tsp"); - } - catch (UnitsNetException e) - { - Assert.True(e.InnerException is AmbiguousUnitParseException); - } + Assert.Throws(() => Volume.Parse("1 tsp")); } [TestCase("m^2", Result = AreaUnit.SquareMeter)] diff --git a/UnitsNet/AmbiguousUnitParseException.cs b/UnitsNet/AmbiguousUnitParseException.cs new file mode 100644 index 0000000000..c9683a780d --- /dev/null +++ b/UnitsNet/AmbiguousUnitParseException.cs @@ -0,0 +1,36 @@ +// Copyright(c) 2007 Andreas Gullberg Larsen +// https://github.com/anjdreas/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; + +namespace UnitsNet +{ + public class AmbiguousUnitParseException : UnitsNetException + { + public AmbiguousUnitParseException(string message) : base(message) + { + } + + public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException) + { + } + } +} \ No newline at end of file diff --git a/UnitsNet/CustomCode/UnitSystem.cs b/UnitsNet/CustomCode/UnitSystem.cs index fb5fd596bc..acf625257c 100644 --- a/UnitsNet/CustomCode/UnitSystem.cs +++ b/UnitsNet/CustomCode/UnitSystem.cs @@ -32,11 +32,6 @@ namespace UnitsNet { - public class AbbreviationMap : Dictionary> - { - - } - [PublicAPI] public partial class UnitSystem { @@ -312,5 +307,13 @@ private void LoadDefaultAbbreviatons([NotNull] IFormatProvider culture) } } } + + /// + /// Avoids having too many nested generics for code clarity + /// + class AbbreviationMap : Dictionary> + { + + } } } \ No newline at end of file diff --git a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 2e97a8bb1d..0201f8d301 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -432,7 +432,11 @@ private static List ParseWithRegex(string regexString, string str, converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index af61d1867c..d84bfdb4e1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -360,7 +360,11 @@ private static List ParseWithRegex(string regexString, string st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index e47a9faa7c..d290f2a202 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 726318e673..a4bc30e0a0 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFormat converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs index 839f6c968f..babfb1e1d1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFor converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index bf8c463258..34e78ce19d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index f7125fafa8..d4d798aa26 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -412,7 +412,11 @@ private static List ParseWithRegex(string regexString, string s converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index a722ba2583..e7ca5fffe2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -392,7 +392,11 @@ private static List ParseWithRegex(string regexString, string converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index ea491f99d8..284be25c22 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -352,7 +352,11 @@ private static List ParseWithRegex(string regexString, strin converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs index c8ae39b475..cc0647082b 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs @@ -552,7 +552,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index 42b467469c..6498af896e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -412,7 +412,11 @@ private static List ParseWithRegex(string regexString, string str, IFormat converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 5c2c200dd4..a9c30be301 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs index 1aea4c3271..49353a8855 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs @@ -312,7 +312,11 @@ private static List ParseWithRegex(string regexString, string s converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 537dda7d9e..2ffe6fc069 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string str, IF converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index dc0def7001..f85f8a74ad 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -812,7 +812,11 @@ private static List ParseWithRegex(string regexString, string str, converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs index 26d841a987..24629aa970 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, strin converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 7846ba599f..723d83de41 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -572,7 +572,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index de8f5d6018..4de3cf60f3 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -340,7 +340,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 6c649b8c52..ea2ae290ee 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -632,7 +632,11 @@ private static List ParseWithRegex(string regexString, string str, IFormat converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs index 2d0faf105e..6da3445f30 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 8c88158567..aef6aea02e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -612,7 +612,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 607ee46d94..d9d4c5d089 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -340,7 +340,11 @@ private static List ParseWithRegex(string regexString, string str, I converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 1d6531cd75..c7b8f90ec2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -952,7 +952,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs index ab502194ae..f4925ec052 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs @@ -372,7 +372,11 @@ private static List ParseWithRegex(string regexString, strin converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index d981186fee..13656b2bd9 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -412,7 +412,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index 7a06a8b558..a7bcff568b 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string s converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs index 39099ca08d..4990f446f6 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs index 9c3420821c..773c21e697 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs @@ -612,7 +612,11 @@ private static List ParseWithRegex(string regexString, string st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index b26772f673..9f480efeaa 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -532,7 +532,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index c6cdbfebee..b09cee2409 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string str, converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs b/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs index cdbbc6777d..9bd160a09e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs @@ -472,7 +472,11 @@ private static List ParseWithRegex(string regexString, st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 9db32f4dac..e24094097b 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -612,7 +612,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs b/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs index 9fd638199d..29c0de57e0 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs @@ -312,7 +312,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index ac984df166..248d2bfb0e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -874,7 +874,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index f7995cc851..0d6502bc1e 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -353,7 +353,11 @@ namespace UnitsNet converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/UnitsNetException.cs b/UnitsNet/UnitsNetException.cs index 3c57360991..ec344ee054 100644 --- a/UnitsNet/UnitsNetException.cs +++ b/UnitsNet/UnitsNetException.cs @@ -37,15 +37,4 @@ public UnitsNetException(string message, Exception innerException) : base(messag { } } - - public class AmbiguousUnitParseException : UnitsNetException - { - public AmbiguousUnitParseException(string message) : base(message) - { - } - - public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException) - { - } - } } \ No newline at end of file