Share via


Error Handling with Enumerations

Question

Thursday, March 4, 2010 11:22 PM

Hello,

It seems to me there really ought to be a better way to match returned error messages with the corresponding enumeration...which usually means there is and I just don't have the knwoledge. My question is this. I make a call to an object method and send some data, it returns an error code. The error code is an INT but comes from an enumeration. I can do a Enum.GetValues(typeof(ErrorEnumeration)) and get an array back. I could then search the array for the matching int value that was returned and get my Error code enumeration.....BUT I STILL have to build a case statement to provide the proper error message back so what's the point? Why not just build my case statement with cast enumerations switching on the error code? Further to the point if my approach to error handling feedback is flawed then please don't just answer the question, slap me around and show me how it's done.

Thank You Very Much
JB

All replies (8)

Friday, March 5, 2010 9:00 AM ✅Answered

Or you can add a Description attribute to the enum values This is an example I had:

public enum CustomerTypeOption 
{ 
    [DescriptionAttribute("Not Specified")] 
    NotDefined, 

    [DescriptionAttribute("Corporation")] 
    Corp, 

    [DescriptionAttribute("Individual or Family")] 
    IndividualorFamily, 

    [DescriptionAttribute("School or University")] 
    SchoolorUniversity, 

    [DescriptionAttribute("Charity")] 
    Charity 
} 

But you could easily  change it to use any text, such as the error description text.

You can then read it using an extension method, like this:

public static string GetDescription(this Enum currentEnum) 
{ 
    string description = String.Empty; 
    DescriptionAttribute da ; 

    FieldInfo fi = currentEnum.GetType(). 
                GetField(currentEnum.ToString()); 
    da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,  
                typeof(DescriptionAttribute)); 
    if (da != null) 
        description = da.Description; 
    else 
        description = currentEnum.ToString(); 

    return description; 
} 

I have a more complete example on my blog.

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!


Friday, March 5, 2010 12:45 PM ✅Answered

Here are a few reasons:

  • Reduces errors caused by transposing or mistyping numbers.

  • Makes it easy to change values in the future.

  • Makes code easier to read, which means it is less likely that errors will creep into it.

  • Ensures forward compatibility. With enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.

Also, you aren't restricted to switch statements...for example you can use enum flags (http://weblogs.asp.net/wim/archive/2004/04/07/109095.aspx ).


Thursday, March 4, 2010 11:30 PM

Hello,

It seems to me there really ought to be a better way to match returned error messages with the corresponding enumeration...which usually means there is and I just don't have the knwoledge. My question is this. I make a call to an object method and send some data, it returns an error code. The error code is an INT but comes from an enumeration. I can do a Enum.GetValues(typeof(ErrorEnumeration)) and get an array back. I could then search the array for the matching int value that was returned and get my Error code enumeration.....BUT I STILL have to build a case statement to provide the proper error message back so what's the point? Why not just build my case statement with cast enumerations switching on the error code? Further to the point if my approach to error handling feedback is flawed then please don't just answer the question, slap me around and show me how it's done.

Thank You Very Much
JB

http://www.codeguru.com/csharp/csharp/cs_syntax/anandctutorials/article.php/c5819Just Be Humble Malange!


Thursday, March 4, 2010 11:30 PM

Maybe you aren't used to working with enums; it is actually very simple to convert an integer to its cooresponding enum.

YourEnum foo = (YourEnum)yourInt;

Friday, March 5, 2010 6:38 AM

I understand what both of you have provided. Enums, integers, how to make them etc. But what I want to know is WHY?? What good are they other than reducing confusion a small amount if you still are going to have to write a switch statement. I mean wouldn't it be cool if I could write this line:

if(ReturnedValue != MyObj.MyEnum.Success)
{
MyEnum retVal = (MyEnum)ReturnedValue;
txtBx ErrMsg.Text = retVal.Message;
}

Instead of this beast:

switch ()
{
     Case MyEnum.MathError:
     txtBxErrMsg.Text= "Some Error Message";
     break;

     case MyEnum.SpellingError:
      txtBxErrMSg.Text = "Some Erro Message";
      break;

      etc.......
}


Friday, March 5, 2010 8:20 AM

If you want to return something with a Text property, you're free to create something with a Text property and return it.


Friday, March 5, 2010 9:33 AM

I have a more complete example on my blog.

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

You seem to have a written a blog post on every scenario imaginable.  :)Ganesh Ranganathan
[Please mark the post as answer if it answers your question]
blog.ganeshzone.net


Saturday, March 6, 2010 4:15 AM

The last one about using Enums for flags was very cool. Not direct answer to the question but very cool variation on what *I* knew of enums.

Thank You
JB