Background
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:
Fields:
- myArray
- myDictionary
- modeIndex = 3
Constructor Methods:
- Entry()
- Entry(UInt32 data)
Public Accessor Methods:
- int GetMode(int index)
- int GetModeDescription
So the way that this class works, is...
- The class is constructed with an array of data.
- The class builds
myArray
as an exact copy of the data with which the class was constructed. - The class constructs a dictionary from a very large XML file, which contains the meanings of the various mode(s).
- The class will then be accessed by an outsider, inquiring about the mode(s).
- If
GetMode
is called, the mode number will be returned (after being extracted from a fixed location withinmyArray
. - If
GetModeDescription
is called, firstGetMode
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.
Question
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:
- Don't do anything about the exception, the user has violated my contract and it's their fault.
- Catch the exception, and rethrow it with the original message plus some additional information about how the number being returned is not valid.
- Just re-throw the exception.
Is one of these acceptable, or is there another way?