diff --git a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs index c4eaf27d68..6ead7a1578 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs @@ -1460,7 +1460,8 @@ internal static TextLoader CreateTextLoader(IHostEnvironment host, var memberInfo = memberInfos[index]; var mappingAttr = memberInfo.GetCustomAttribute(); - host.Assert(mappingAttr != null, $"Field or property {memberInfo.Name} is missing the {nameof(LoadColumnAttribute)} attribute"); + if (mappingAttr == null) + throw host.Except($"{(memberInfo is FieldInfo ? "Field" : "Property")} '{memberInfo.Name}' is missing the {nameof(LoadColumnAttribute)} attribute"); var mappingAttrName = memberInfo.GetCustomAttribute(); diff --git a/test/Microsoft.ML.Tests/TextLoaderTests.cs b/test/Microsoft.ML.Tests/TextLoaderTests.cs index 06f455d8f7..e22df9aabb 100644 --- a/test/Microsoft.ML.Tests/TextLoaderTests.cs +++ b/test/Microsoft.ML.Tests/TextLoaderTests.cs @@ -589,13 +589,8 @@ public void CanSuccessfullyTrimSpaces() public void ThrowsExceptionWithPropertyName() { var mlContext = new MLContext(seed: 1); - try - { - mlContext.Data.LoadFromTextFile("fakefile.txt"); - } - // REVIEW: the issue of different exceptions being thrown is tracked under #2037. - catch (Xunit.Sdk.TrueException) { } - catch (NullReferenceException) { }; + var ex = Assert.Throws(() => mlContext.Data.LoadFromTextFile("fakefile.txt")); + Assert.StartsWith($"Field 'String1' is missing the {nameof(LoadColumnAttribute)} attribute", ex.Message); } [Fact]