How do you maintain code with InvalidEnumArgumentException?

I am curious how you will maintain your code as soon as you throw a System.ComponentModel.InvalidEnumArgumentException .

Basically, I have a switch statement:

 switch (enumValue) { case MyEnum.Value1: break; case MyEnum.Value2: break; default: throw new InvalidEnumArgumentException(); } 

What if I want to add more values ​​to MyEnum in the future, for example, Value3 and Value4 ? That would mean that I would eventually throw an erroneous exception. How can I prevent this?

Should I use reflection before throwing? What exception should I make in this case? I am looking for offers.


I just discovered this exception a couple of minutes ago, so maybe I am considering this in the wrong context. Is this exception Value3 if a specific enum argument is not supported (in this case, Value3 and Value4 not supported)?

+6
source share
6 answers

The problem you are talking about depends on the context, if a method receives an enumeration as an argument, it must indicate what values ​​it supports, and what it does with an unknown enumeration value.

If you add additional enumeration options, you need to decide what to do, even if you did not select an exception in the default case.

Recall that an exception is especially useful since you can pass any integer as an enum value.

For instance:

 enum Foo { A, B } static int Bar(Foo f) { switch (f) { case Foo.A: return 1; case Foo.B: return 2; default: throw new InvalidEnumArgumentException("f", (int)f, typeof(Foo)); } } static void Main() { Bar(Foo.A); Bar((Foo)99); } 
+8
source

What if I want to add more values ​​to MyEnum in the future, such as Value3 and Value4? That would mean that I would eventually throw an erroneous exception. How can I prevent this?

When you use an InvalidEnumArgumentException , the key to understanding is the argument. Throwing an exception, you say that the argument to the method is invalid. ( InvalidEnumArgumentException comes from ArgumentException .) This does not necessarily mean that the value was not a member of enum . Therefore, I do not find this misleading.

+4
source

The exception you are using is incorrect:

http://msdn.microsoft.com/en-us/library/system.componentmodel.invalidenumargumentexception.aspx

This exception is thrown if you pass an invalid enumeration value to a method or when setting a property.

+2
source

To check the input value of an enum, use the following static method ...

 public void MyMethod(MyEnum e) { if (!Enum.IsDefined(typeof(MyEnum), e)) throw new InvalidEnumArgumentException("e", (int)e, typeof(MyEnum)); 

... and you don’t need to change the validation if new enum values ​​are added in the future.

+1
source

I would not use this exception, which you use in this context. Due to the fact that enumValue is of type MyEnum (suppose?), It can never contain an invalid enum value. If you have switches based on the value of the enumeration that need to fail if they do not recognize the value, then you will need to throw an appropriate exception (maybe just a normal ArgumentException?), But in most cases, I think you let the code run does nothing for an unknown enumeration value.

0
source

I believe that you are looking in the wrong context, if this is not an enumeration, but some specific values ​​that were based on some kind of business rule, so in cases where the rules increase, you should correct the corresponding codes like these so that include new ones. Therefore, if you change Enum, then you should look for changes like these.

0
source

Source: https://habr.com/ru/post/891804/


All Articles