Overflow Exception Checking Problem


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?


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.


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *