Catching an int overflow error

This is a discussion on Catching an int overflow error within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I've got some code where I'm calculating a geometric mean: Code: include <exception> using std:: overflow_error // space ...

  1. #1
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76

    Question Catching an int overflow error

    Hey everyone, I've got some code where I'm calculating a geometric mean:

    Code:
    include <exception>
    using std:: overflow_error  // space to prevent a smiley!
    ...
    try {
      // Calculate the geometric mean of the values in array prevBids
      int product = 1;
      for (int i = 0; i < maxPrevBids; i++) {
        product *= prevBids[i];
      }
      bid = (static_cast<int>( pow (product, ( 1.00 / static_cast<double>(maxPrevBids)))));
    } catch (overflow_error &e) { 
      // Some error-handling code
    }
    Now sometimes the value of product gets too big for an integer, and I want to somehow catch this error when it happens. I could change it to a long, but I'd still want to catch the overflow error if it happened with a long, so the type of product doesn't really matter to my question. (I can potentially get all sorts of crazy numbers in this code and I need to be able to deal with them all!)

    I tried enclosing the code in a try/catch block like shown above, but when I stepped through the code with the debugger, it didn't throw an exception. It just kept trying to multiply product (resulting in some very weird values for product). If product was an invalid entry for the pow function, then an exception was thrown at the pow function.

    Any ideas on how I can catch or prevent this overflow error?
    My programs don't have bugs, they just develop random features.

  2. #2
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    That is amazing -- I changed it to a double and I never got that error (at least with the parameters I ran the program with). I guess a double can hold larger numbers than an int?

    Still, I'm curious if anyone knows how to catch overflow errors like I mentioned in my first post, because that's something that I'm sure other people may find useful as well!
    My programs don't have bugs, they just develop random features.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In C++, there is no such thing as an overflow. If you keep looping with the for loop for( int i = 0; ; i++ ); the value of i would eventually become negative.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Then what's the overflow_error I found in the exception class in the STL? Is it more like a stack overflow?
    My programs don't have bugs, they just develop random features.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can put in your own testing routines:

    Code:
    int multiply(int a, int b)
    {
       if(a > (MAX_INT / b))
          throw new my_overflow_error_exception(__LINE__);
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    A couple of things for you to look-up...

    I guess a double can hold larger numbers than an int?
    Check-out sizeof() and the <limits> header file. The C++ standard specifies a minimum "capacity" for each type. The actual size of each variable is system dependent.
    In C++, there is no such thing as an overflow. If you keep looping with the for loop for( int i = 0; ; i++ ); the value of i would eventually become negative.
    Well, I'd call that an overflow error! (Unless it's what you wanted.) Also an unsigned int won't go negative. It will start-over at zero like an odometer rolling-over.

    [EDIT]
    Once, I was working on a problem with a system that plotted pressure (psi) on a chart recorder. At a particular pressure, the chart-recorder would go erratic. With the Analog-to-Digital conversion from the pressure sensor, then Digital-to-Analog conversion for the chart recorder, we couldn't see the count that was overflowing in binary and it was very difficult to determine that it was an overflow problem!
    Last edited by DougDbug; 06-10-2003 at 02:42 PM.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    148
    19.1.8 class overflow_error [lib.overflow.error]
    The class overflow_error defines the type of objects thrown as exceptions to report an arithmetic overflow error.
    But there are very few methodes actually throwing overflow_error.
    For example
    bitset<N>::to_ulong() const;
    Throws: overflow_error if the integral value can not represented as type unsigend long

  8. #8
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    thanks everyone... very helpful info
    My programs don't have bugs, they just develop random features.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21