Skip to content

Commit ab472d2

Browse files
committed
use ArgumentException instead of a custom exception
1 parent c41ff07 commit ab472d2

File tree

2 files changed

+15
-45
lines changed

2 files changed

+15
-45
lines changed

src/CountExceedingBehaviour.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23

34
namespace SpanExtensions
45
{
@@ -22,12 +23,14 @@ public enum CountExceedingBehaviour
2223
/// </summary>
2324
static class CountExceedingBehaviourExtensions
2425
{
26+
static string? CountExceedingBehaviourInvalidFormat;
27+
2528
/// <summary>
2629
/// Validates whether a specified value is a valid <see cref="CountExceedingBehaviour"/>.
2730
/// </summary>
2831
/// <param name="countExceedingBehaviour">The <see cref="CountExceedingBehaviour"/> to validate.</param>
2932
/// <returns>The specified <see cref="CountExceedingBehaviour"/> if valid.</returns>
30-
/// <exception cref="InvalidCountExceedingBehaviourException">If <paramref name="countExceedingBehaviour"/> is not valid.</exception>
33+
/// <exception cref="ArgumentException">If <paramref name="countExceedingBehaviour"/> is not valid.</exception>
3134
public static CountExceedingBehaviour ThrowIfInvalid(this CountExceedingBehaviour countExceedingBehaviour)
3235
{
3336
#if NET5_0_OR_GREATER
@@ -36,7 +39,17 @@ public static CountExceedingBehaviour ThrowIfInvalid(this CountExceedingBehaviou
3639
if(!Enum.IsDefined(typeof(CountExceedingBehaviour), countExceedingBehaviour))
3740
#endif
3841
{
39-
throw new InvalidCountExceedingBehaviourException(countExceedingBehaviour);
42+
if(CountExceedingBehaviourInvalidFormat == null)
43+
{
44+
#if NET5_0_OR_GREATER
45+
string[] names = Enum.GetNames<CountExceedingBehaviour>();
46+
#else
47+
string[] names = (string[]) Enum.GetNames(typeof(CountExceedingBehaviour));
48+
#endif
49+
CountExceedingBehaviourInvalidFormat = $"{nameof(CountExceedingBehaviour)} doesn't define an option with the value '{{0}}'. Valid values are {string.Join(", ", names)}.";
50+
}
51+
52+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, CountExceedingBehaviourInvalidFormat, (int)countExceedingBehaviour), nameof(countExceedingBehaviour));
4053
}
4154

4255
return countExceedingBehaviour;

src/InvalidCountExceedingBehaviourException.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)