I have to call a method that needs to return a signed integer (see code block below) after converting from an unsigned integer. The reason for this is that I have to do bit-wise math that relies on the value starting out as an unsigned integer.

I was thinking of doing the try/catch with checking method that MSDN describes here. My only problem with this method is that, while I do want to handle the exception in an appropriate manner... There is no real predicting what meanings that the numbers might have for the caller. What I mean is, whoever calls GetMode will use the value to look up a string that provides a further meaning of what the mode number actually means.

mode = object.GetMode(3);

Where GetMode is defined as follows.

public int GetMode(int index)
    UInt32 modeUint = 0;
    int modeInt = 0;

    // extract the number from the array
    modeUint = this.myArray[index];

    // convert to a signed integer
    modeInt = Convert.ToInt32(modeUint); 

    return modeInt;

The Class Structure

The class is structured in the following way:


  1. myArray
  2. myDictionary
  3. modeIndex = 3

Constructor Methods:

  1. Entry()
  2. Entry(UInt32 data)

Public Accessor Methods:

  1. int GetMode(int index)
  2. int GetModeDescription

So the way that this class works, is...

  1. The class is constructed with an array of data.
  2. The class builds myArray as an exact copy of the data with which the class was constructed.
  3. The class constructs a dictionary from a very large XML file, which contains the meanings of the various mode(s).
  4. The class will then be accessed by an outsider, inquiring about the mode(s).
  5. If GetMode is called, the mode number will be returned (after being extracted from a fixed location within myArray.
  6. If GetModeDescription is called, first GetMode will be used to get the number associated with the mode. This number will in-turn be used as the key to look up a value from the dictionary in order to obtain the appropriate string description.


I want to prevent mis-using one of the mode (i.e. catch the exception, return 0). So I was thinking of doing one of the following:

  1. Don't do anything about the exception, the user has violated my contract and it's their fault.
  2. Catch the exception, and rethrow it with the original message plus some additional information about how the number being returned is not valid.
  3. Just re-throw the exception.

Is one of these acceptable, or is there another way?

  • 1
    I would say replace the int parameter with an enum of some type to prevent the user from misusing the method + 2.
    – Zymus
    Apr 4, 2016 at 16:08
  • @Zymus That's an idea... because all of the possible mode numbers will have designated meanings. So I could probably do that, but my concern is still... what if I get bad data and the overflow happens? Do I catch it and use an enum for that as well?
    – Snoop
    Apr 4, 2016 at 16:12
  • It might be good to see the whole class. In particular, I would like to understand how (the potentially bad) values get into the array. It feels like the class has insufficient responsibility or is simply too low level.
    – Erik Eidt
    Apr 4, 2016 at 16:26
  • @ErikEidt I don't usually like to pack the question with too much detail until specifically asked about it, such as in your case where you feel like more information is necessary. So I will do my best to update this question and address each point you've asked about.
    – Snoop
    Apr 4, 2016 at 16:29

1 Answer 1


If I understand correctly, your Question is if and how to check whether

// convert to a signed integer
modeInt = Convert.ToInt32(modeUint); 

throws an OverflowException because modeUint = this.myArray[index]; contains an unsigned integer that is too large to fit.

I offer the following:

  • Do not check here, because the erroneous data is already loaded into a member of your object (myArray)

  • Do check in the constructor, where:

The class builds myArray as an exact copy of the data with which the class was constructed.

and throw an appropriate exception there.

  • Maybe something like, "CorruptedDataException" with a message saying something about not being able to do any further processing?
    – Snoop
    Apr 4, 2016 at 18:46
  • @StevieV - What you call it don't matter, but it should include the details of the corrupt data and the index of where it is located in the passed array (see: programmers.stackexchange.com/questions/278949/…)
    – Martin Ba
    Apr 4, 2016 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.