Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 0101324

Browse files
MarcoRossignolisafern
authored andcommitted
"Don't directly throw Exception" System.ComponentModel.TypeConverter (#26050)
* Don't directly throw Exception * Don't directly throw Exception
1 parent baa9ed4 commit 0101324

17 files changed

+46
-53
lines changed

src/Common/src/System/Drawing/ColorConverterCommon.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static int IntFromString(string text, CultureInfo culture)
128128
}
129129
catch (Exception e)
130130
{
131-
throw new Exception(SR.Format(SR.ConvertInvalidPrimitive, text, typeof(int).Name), e);
131+
throw new ArgumentException(SR.Format(SR.ConvertInvalidPrimitive, text, typeof(int).Name), e);
132132
}
133133
}
134134

@@ -142,4 +142,4 @@ private static int IntFromString(string value, NumberFormatInfo formatInfo)
142142
return Int32.Parse(value, NumberStyles.Integer, formatInfo);
143143
}
144144
}
145-
}
145+
}

src/System.ComponentModel.Annotations/tests/RangeAttributeTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override IEnumerable<TestCase> ValidValues()
3636
yield return new TestCase(stringIntRange, "2");
3737
yield return new TestCase(stringIntRange, 3);
3838
yield return new TestCase(stringIntRange, "3");
39-
39+
4040
RangeAttribute stringDoubleRange = new RangeAttribute(typeof(double), (1.0).ToString("F1"), (3.0).ToString("F1"));
4141
yield return new TestCase(stringDoubleRange, null);
4242
yield return new TestCase(stringDoubleRange, string.Empty);
@@ -91,8 +91,8 @@ protected override IEnumerable<TestCase> InvalidValues()
9191
public static void Validate_CantConvertValueToTargetType_ThrowsException(Type type, string minimum, string maximum)
9292
{
9393
var attribute = new RangeAttribute(type, minimum, maximum);
94-
Assert.Throws<Exception>(() => attribute.Validate("abc", new ValidationContext(new object())));
95-
Assert.Throws<Exception>(() => attribute.IsValid("abc"));
94+
AssertExtensions.Throws<ArgumentException, Exception>(() => attribute.Validate("abc", new ValidationContext(new object())));
95+
AssertExtensions.Throws<ArgumentException, Exception>(() => attribute.IsValid("abc"));
9696
}
9797

9898
[Fact]
@@ -168,7 +168,7 @@ public static void Validate_MinimumOrMaximumNull_ThrowsInvalidOperationException
168168
public static void Validate_MinimumOrMaximumCantBeConvertedToIntegralType_ThrowsException(Type type, string minimum, string maximum)
169169
{
170170
RangeAttribute attribute = new RangeAttribute(type, minimum, maximum);
171-
Assert.Throws<Exception>(() => attribute.Validate("Any", new ValidationContext(new object())));
171+
AssertExtensions.Throws<ArgumentException, Exception>(() => attribute.Validate("Any", new ValidationContext(new object())));
172172
}
173173

174174
[Theory]

src/System.ComponentModel.TypeConverter/src/System/ComponentModel/BaseNumberConverter.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,7 @@ internal abstract Type TargetType
3434
/// Convert the given value to a string using the given formatInfo
3535
/// </summary>
3636
internal abstract object FromString(string value, NumberFormatInfo formatInfo);
37-
38-
/// <summary>
39-
/// Create an error based on the failed text and the exception thrown.
40-
/// </summary>
41-
internal virtual Exception FromStringError(string failedText, Exception innerException)
42-
{
43-
return new Exception(SR.Format(SR.ConvertInvalidPrimitive, failedText, TargetType.Name), innerException);
44-
}
45-
37+
4638
/// <summary>
4739
/// Convert the given value from a string using the given formatInfo
4840
/// </summary>
@@ -90,7 +82,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
9082
}
9183
catch (Exception e)
9284
{
93-
throw FromStringError(text, e);
85+
throw new ArgumentException(SR.Format(SR.ConvertInvalidPrimitive, text, TargetType.Name), nameof(value), e);
9486
}
9587
}
9688
return base.ConvertFrom(context, culture, value);

src/System.ComponentModel.TypeConverter/tests/ByteConvertersTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void ConvertFrom_WithContext()
5353
[Fact]
5454
public static void ConvertFrom_WithContext_Negative()
5555
{
56-
Assert.Throws<Exception>(
56+
AssertExtensions.Throws<ArgumentException, Exception>(
5757
() => ByteConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "8.0"));
5858
}
5959

src/System.ComponentModel.TypeConverter/tests/DecimalConverterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public static void ConvertFrom_WithContext()
2525
[Fact]
2626
public static void ConvertFrom_WithContext_Negative()
2727
{
28-
Assert.Throws<Exception>(
29-
() => DecimalConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "0x8"));
28+
AssertExtensions.Throws<ArgumentException, Exception>(
29+
() => DecimalConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "0x8"));
3030
}
3131

3232
[Fact]

src/System.ComponentModel.TypeConverter/tests/DoubleConverterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void ConvertFrom_WithContext()
2525
[Fact]
2626
public static void ConvertFrom_WithContext_Negative()
2727
{
28-
Assert.Throws<Exception>(
28+
AssertExtensions.Throws<ArgumentException, Exception>(
2929
() => DoubleConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "0x8"));
3030
}
3131

src/System.ComponentModel.TypeConverter/tests/Drawing/ColorConverterTests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static IEnumerable<object[]> ColorData
7676
{
7777
for (int b = 0; b < 256; b += 67)
7878
{
79-
yield return new object[] {a, r, g, b};
79+
yield return new object[] { a, r, g, b };
8080
}
8181
}
8282
}
@@ -86,25 +86,25 @@ public static IEnumerable<object[]> ColorData
8686

8787
public static IEnumerable<object[]> ColorNames => typeof(Color).GetProperties()
8888
.Where(p => p.PropertyType == typeof(Color))
89-
.Select(p => new object[] { p.Name} );
89+
.Select(p => new object[] { p.Name });
9090

9191
[Theory]
9292
[MemberData(nameof(ColorData))]
9393
public void ConvertFrom(int a, int r, int g, int b)
9494
{
9595
var conv = new ColorConverter();
96-
Color color = (Color) conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"#0x{a:x2}{r:x2}{g:x2}{b:x2}");
96+
Color color = (Color)conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"#0x{a:x2}{r:x2}{g:x2}{b:x2}");
9797
Assert.Equal(a, color.A);
9898
Assert.Equal(r, color.R);
9999
Assert.Equal(g, color.G);
100100
Assert.Equal(b, color.B);
101101

102102
Assert.Equal(color,
103-
(Color) conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"#0X{a:x2}{r:x2}{g:x2}{b:x2}"));
103+
(Color)conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"#0X{a:x2}{r:x2}{g:x2}{b:x2}"));
104104
Assert.Equal(color,
105-
(Color) conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"0x{a:x2}{r:x2}{g:x2}{b:x2}"));
105+
(Color)conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"0x{a:x2}{r:x2}{g:x2}{b:x2}"));
106106
Assert.Equal(color,
107-
(Color) conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"0X{a:x2}{r:x2}{g:x2}{b:x2}"));
107+
(Color)conv.ConvertFrom(null, CultureInfo.InvariantCulture, $"0X{a:x2}{r:x2}{g:x2}{b:x2}"));
108108
}
109109

110110
[Theory]
@@ -146,17 +146,17 @@ public void ConvertFrom_Name(string name)
146146
{
147147
var conv = new ColorConverter();
148148
var color = Color.FromName(name);
149-
Assert.Equal(color, (Color) conv.ConvertFrom(name));
150-
Assert.Equal(color, (Color) conv.ConvertFrom(" " + name + " "));
149+
Assert.Equal(color, (Color)conv.ConvertFrom(name));
150+
Assert.Equal(color, (Color)conv.ConvertFrom(" " + name + " "));
151151
}
152152

153153
[Fact]
154154
public void ConvertFrom_Empty()
155155
{
156156
var conv = new ColorConverter();
157157
var color = Color.Empty;
158-
Assert.Equal(color, (Color) conv.ConvertFrom(string.Empty));
159-
Assert.Equal(color, (Color) conv.ConvertFrom(" "));
158+
Assert.Equal(color, (Color)conv.ConvertFrom(string.Empty));
159+
Assert.Equal(color, (Color)conv.ConvertFrom(" "));
160160
}
161161

162162
[Theory]
@@ -177,7 +177,7 @@ public void ConvertFrom_ArgumentException(string value)
177177
public void ConvertFrom_Exception(string value)
178178
{
179179
var conv = new ColorConverter();
180-
Assert.Throws<Exception>(() =>
180+
AssertExtensions.Throws<ArgumentException, Exception>(() =>
181181
{
182182
conv.ConvertFrom(null, CultureInfo.InvariantCulture, value);
183183
});
@@ -208,7 +208,7 @@ public void ConvertFrom_NotSupportedException(object value)
208208
public void ConvertFrom_NullCulture()
209209
{
210210
var conv = new ColorConverter();
211-
var color = (Color) conv.ConvertFrom(null, null, "#0x23190A44");
211+
var color = (Color)conv.ConvertFrom(null, null, "#0x23190A44");
212212
Assert.Equal(35, color.A);
213213
Assert.Equal(25, color.R);
214214
Assert.Equal(10, color.G);
@@ -221,7 +221,7 @@ public void ConvertTo(int a, int r, int g, int b)
221221
{
222222
var conv = new ColorConverter();
223223
Assert.Equal($"{a}, {r}, {g}, {b}",
224-
(string) conv.ConvertTo(null, CultureInfo.InvariantCulture, Color.FromArgb(a, r, g, b), typeof(string)));
224+
(string)conv.ConvertTo(null, CultureInfo.InvariantCulture, Color.FromArgb(a, r, g, b), typeof(string)));
225225
}
226226

227227
[Theory]
@@ -230,15 +230,15 @@ public void ConvertTo_Named(string name)
230230
{
231231
var conv = new ColorConverter();
232232
Assert.Equal(name,
233-
(string) conv.ConvertTo(null, CultureInfo.InvariantCulture, Color.FromName(name), typeof(string)));
233+
(string)conv.ConvertTo(null, CultureInfo.InvariantCulture, Color.FromName(name), typeof(string)));
234234
}
235235

236236
[Fact]
237237
public void ConvertTo_Empty()
238238
{
239239
var conv = new ColorConverter();
240240
Assert.Equal(string.Empty,
241-
(string) conv.ConvertTo(null, CultureInfo.InvariantCulture, Color.Empty, typeof(string)));
241+
(string)conv.ConvertTo(null, CultureInfo.InvariantCulture, Color.Empty, typeof(string)));
242242
}
243243

244244
[Theory]
@@ -306,7 +306,7 @@ public void GetProperties()
306306
public void ConvertFromInvariantString(int a, int r, int g, int b)
307307
{
308308
var conv = new ColorConverter();
309-
var color = (Color) conv.ConvertFromInvariantString($"{a}, {r}, {g}, {b}");
309+
var color = (Color)conv.ConvertFromInvariantString($"{a}, {r}, {g}, {b}");
310310
Assert.Equal(a, color.A);
311311
Assert.Equal(r, color.R);
312312
Assert.Equal(g, color.G);
@@ -319,7 +319,7 @@ public void ConvertFromInvariantString_Name(string name)
319319
{
320320
var conv = new ColorConverter();
321321
var color = Color.FromName(name);
322-
Assert.Equal(color, (Color) conv.ConvertFromInvariantString(name));
322+
Assert.Equal(color, (Color)conv.ConvertFromInvariantString(name));
323323
}
324324

325325
[Fact]
@@ -336,7 +336,7 @@ public void ConvertFromInvariantString_Invalid()
336336
public void ConvertFromInvariantString_NotNumber()
337337
{
338338
var conv = new ColorConverter();
339-
var ex = Assert.Throws<Exception>(() =>
339+
var ex = AssertExtensions.Throws<ArgumentException, Exception>(() =>
340340
{
341341
conv.ConvertFromInvariantString("hello");
342342
});
@@ -365,7 +365,7 @@ public void ConvertFromString_Name(string name)
365365
{
366366
var conv = new ColorConverter();
367367
var color = Color.FromName(name);
368-
Assert.Equal(color, (Color) conv.ConvertFromString(name));
368+
Assert.Equal(color, (Color)conv.ConvertFromString(name));
369369
}
370370

371371
[Fact]
@@ -382,7 +382,7 @@ public void ConvertFromString_Invalid()
382382
public void ConvertFromString_NotNumber()
383383
{
384384
var conv = new ColorConverter();
385-
var ex = Assert.Throws<Exception>(() =>
385+
var ex = AssertExtensions.Throws<ArgumentException, Exception>(() =>
386386
{
387387
conv.ConvertFromString("hello");
388388
});

src/System.ComponentModel.TypeConverter/tests/Drawing/StringTypeConverterTestBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected void ConvertFromThrowsArgumentExceptionForString(string value)
8888

8989
protected void ConvertFromThrowsFormatInnerExceptionForString(string value)
9090
{
91-
var ex = Assert.Throws<Exception>(() =>
91+
var ex = AssertExtensions.Throws<ArgumentException, Exception>(() =>
9292
{
9393
Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
9494
});
@@ -145,7 +145,7 @@ protected void ConvertFromInvariantStringThrowsArgumentException(string str)
145145

146146
protected void ConvertFromInvariantStringThrowsFormatInnerException(string str)
147147
{
148-
var ex = Assert.Throws<Exception>(() =>
148+
var ex = AssertExtensions.Throws<ArgumentException, Exception>(() =>
149149
{
150150
Converter.ConvertFromInvariantString(str);
151151
});
@@ -163,7 +163,7 @@ protected void ConvertFromStringThrowsArgumentException(string str)
163163

164164
protected void ConvertFromStringThrowsFormatInnerException(string str)
165165
{
166-
var ex = Assert.Throws<Exception>(() =>
166+
var ex = AssertExtensions.Throws<ArgumentException, Exception>(() =>
167167
{
168168
Converter.ConvertFromString(str);
169169
});

src/System.ComponentModel.TypeConverter/tests/Int16ConverterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public static void ConvertFrom_WithContext()
2626
[Fact]
2727
public static void ConvertFrom_WithContext_Negative()
2828
{
29-
Assert.Throws<Exception>(
30-
() => Int16ConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "8.0"));
29+
AssertExtensions.Throws<ArgumentException, Exception>(
30+
() => Int16ConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "8.0"));
3131
}
3232

3333
[Fact]

src/System.ComponentModel.TypeConverter/tests/Int32ConverterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void ConvertFrom_WithContext()
2626
[Fact]
2727
public static void ConvertFrom_WithContext_Negative()
2828
{
29-
Assert.Throws<Exception>(
29+
AssertExtensions.Throws<ArgumentException, Exception>(
3030
() => Int32ConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "8.0"));
3131
}
3232

0 commit comments

Comments
 (0)