c# - How do you maintain code with InvalidEnumArgumentException? -


i curious how maintain code once throw system.componentmodel.invalidenumargumentexception.

basically have switch statement this:

switch (enumvalue) {     case myenum.value1:         break;      case myenum.value2:         break;      default:         throw new invalidenumargumentexception(); } 

what if decide add more values myenum in future, example, value3 , value4? mean end throwing misleading exception. how prevent this?

should use reflection before throwing? exception should throw in case? i'm looking suggestions.


i discovered exception couple minutes ago maybe looking @ in wrong context. exception thrown when enum argument not supported (in case value3 , value4 not supported)?

the problem state depends on context, if method receives enumeration argument must specify values support , unknown enumeration value.

if add more enumeration options need decide if not throwing exception in default case.

be reminded exception special helpful since can pass integer enumeration value.

for example:

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); } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -