Thread: Overflow exception unhandles

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    You have a loop going through the individual parts of what is a string.

    What if the string isn't 10 characters long? The loop will try and read past the end of the string, which doesn't exist, and there you get an overflow error. Your best bet is to use:

    Code:
    list<byte> binaryValue = new List<byte>();
    
    foreach(byte x in Convert.ToByte(yourText))
    {
      binaryValue.add(x);
    }
    And use a switch instead of a huge list of If statements, much cleaner.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question

    Hi DanFraser


    Thankyou very much for your reply. Will it be an optimal solution if i use switch cases?

    Can i convert string to hexadecimal directly in c sharp?

    Code:
    if (this.lvItem.SubItems[1].Text == "BINARY")
                {
                    byte[] binaryValue = new byte[10];
                   
                   
                    for(int i = 0 ; i <10 ; i++)
                    {
                        binaryValue[i] = Convert.ToByte(editBox.Text)); 
                   
    
                    }
                    ManagedRegistry.Delete(nodeFullPath, selectedItem.Text);
                    ManagedRegistry.WriteKeyBinary(nodeFullPath, keyName, binaryValue);
                    
                }

    Pls edit the code and reply me with the corrected code as i am unable to understand your logic. Pls help me as am a beginner to C# programming.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    when i change the value of string of characters to 5, it still shows

    OverFlow exception was Unhandled
    Value was either too large or too small for an unsigned byte.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    why don't you use try-catch to see what you have? Is the value in the string from 0-255?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM