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?
-
1I would say replace the int parameter with an enum of some type to prevent the user from misusing the method + 2.Zymus– Zymus04/04/2016 16:08:43Commented 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– Snoop04/04/2016 16:12:58Commented 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– Erik Eidt04/04/2016 16:26:19Commented 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– Snoop04/04/2016 16:29:26Commented 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– Snoop04/04/2016 18:46:52Commented 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– Martin Ba04/04/2016 18:58:34Commented Apr 4, 2016 at 18:58
Explore related questions
See similar questions with these tags.