Overflow exception on a return?

Discussion in 'C++' started by Victor Hannak, Aug 21, 2003.

  1. I have a class with the following public function:

    float FuncClass::GetResult() {
    try {
    return(Result);
    } catch (...) {
    WriteString("FuncClass::GetResult ERROR: Exception encountered");
    return(0);
    }
    }

    Result is a private variable of type float. I am repeatedly seeing the
    catch portion being executed from the above code, and my Borland debugger is
    telling me that it is an EOverflow exception.

    How is it possible that such an exception is triggered by a return
    statement. It would seem logical that if an overflow situation occurrs, it
    would happen when I am assigning (an addition) to the Result variable, but
    not when I am returning the Result variable. How is this possible?
     
    Victor Hannak, Aug 21, 2003
    #1
    1. Advertising

  2. Victor Hannak

    Attila Feher Guest

    Victor Hannak wrote:
    > I have a class with the following public function:
    >
    > float FuncClass::GetResult() {
    > try {
    > return(Result);
    > } catch (...) {
    > WriteString("FuncClass::GetResult ERROR: Exception encountered");
    > return(0);
    > }
    > }
    >
    > Result is a private variable of type float. I am repeatedly seeing
    > the catch portion being executed from the above code, and my Borland
    > debugger is telling me that it is an EOverflow exception.
    >
    > How is it possible that such an exception is triggered by a return
    > statement. It would seem logical that if an overflow situation
    > occurrs, it would happen when I am assigning (an addition) to the
    > Result variable, but not when I am returning the Result variable.
    > How is this possible?


    First of all I guess this is not a standard mandated behavior. The second
    (guess, since you did not post the code, only a fragment) is that you return
    a double as a flot - and it just does not fit.

    Attila
     
    Attila Feher, Aug 21, 2003
    #2
    1. Advertising

  3. Actually, Result _is_ of type float. That was the first thing I thought of.

    I'm not sure how much more code I need to post, after all, the segment I
    included is the portion that's causing the exception. I would think that no
    matter what my program does (to the result variable) before this code is
    called should be irrelevant.

    Anyone have any suggestion on what I could do to try to figure out what's
    going on?

    Vic

    > First of all I guess this is not a standard mandated behavior. The second
    > (guess, since you did not post the code, only a fragment) is that you

    return
    > a double as a flot - and it just does not fit.
    >
     
    Victor Hannak, Aug 22, 2003
    #3
  4. Victor Hannak

    Adam Fineman Guest

    Victor,

    My answer may seem harsh to you, but please bear in mind that my aim is
    to provide you with some guidelines for effectively getting meaningful
    help from this group.

    Top-posting fixed. Please don't top-post on usenet. Look at this:
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.4

    Victor Hannak wrote:
    > Atilla wrote:

    <snip>
    >> since you did not post the code, only a fragment)<snip>

    > I'm not sure how much more code I need to post, after all, the segment I
    > included is the portion that's causing the exception.


    What ever you post, it should be compilable. That means that we should
    be able to cut and paste exactly what you've posted and compile it and
    see the same problem you're seeing. This will require some effort on
    your part, as you will have to isolate the problem. The upside is that
    this is often enough to figure out what the problem is without waiting
    for a response on usenet.

    > I would think that no
    > matter what my program does (to the result variable) before this code is
    > called should be irrelevant.
    > <snip>


    This is almost certainly not true. I've added a minimal amount of code
    required to turn your fragment into a compilable unit (see below), and
    no exception is thrown. That seems to indicate that the problem is not
    in the fragment you posted.

    -----------------
    #include <iostream>

    class FuncClass
    {
    public:
    FuncClass(float r) : Result(r) {}

    float GetResult();

    private:
    void WriteString(const char* s)
    {
    std::cerr << s << '\n';
    }

    float Result;
    };


    float FuncClass::GetResult() {
    try {
    return(Result);
    } catch (...) {
    WriteString("FuncClass::GetResult ERROR: Exception encountered");
    return(0);
    }
    }

    int
    main()
    {
    FuncClass fc(1.0);
    std::cout << fc.GetResult() << '\n';

    return 0;
    }
    -----------------

    - Adam
     
    Adam Fineman, Aug 22, 2003
    #4
  5. I have been unable to isolate _or_ reproduce the problem in a simple
    testcase. That is why I posted to the group in the first place... I was
    hoping for a response from someone saying something like: "I had that same
    problem before when I didn't initialize the class public variable I was
    returning".

    > What ever you post, it should be compilable. That means that we should
    > be able to cut and paste exactly what you've posted and compile it and
    > see the same problem you're seeing. This will require some effort on
    > your part, as you will have to isolate the problem. The upside is that
    > this is often enough to figure out what the problem is without waiting
    > for a response on usenet.
    >


    I think you've missed my point here. I completely agree that the problem is
    not with the posted fragment (otherwise, I could generate a testcase). The
    question is what could I possibly be doing wrong prior to the return that
    would cause an exception when the return is called, while not causing an
    exception prior to the return.

    > > I would think that no
    > > matter what my program does (to the result variable) before this code is
    > > called should be irrelevant.
    > > <snip>

    >
    > This is almost certainly not true. I've added a minimal amount of code
    > required to turn your fragment into a compilable unit (see below), and
    > no exception is thrown. That seems to indicate that the problem is not
    > in the fragment you posted.
    >
     
    Victor Hannak, Aug 23, 2003
    #5
  6. Top posting fixed...my apologies...

    "Victor Hannak" <victor.hannak@> wrote in message
    news:p3O1b.3830$...
    > > What ever you post, it should be compilable. That means that we should
    > > be able to cut and paste exactly what you've posted and compile it and
    > > see the same problem you're seeing. This will require some effort on
    > > your part, as you will have to isolate the problem. The upside is that
    > > this is often enough to figure out what the problem is without waiting
    > > for a response on usenet.
    > >

    > I have been unable to isolate _or_ reproduce the problem in a simple
    > testcase. That is why I posted to the group in the first place... I was
    > hoping for a response from someone saying something like: "I had that same
    > problem before when I didn't initialize the class public variable I was
    > returning".
    >
    > > > I would think that no
    > > > matter what my program does (to the result variable) before this code

    is
    > > > called should be irrelevant.
    > > > <snip>

    > >
    > > This is almost certainly not true. I've added a minimal amount of code
    > > required to turn your fragment into a compilable unit (see below), and
    > > no exception is thrown. That seems to indicate that the problem is not
    > > in the fragment you posted.
    > >

    >
    > I think you've missed my point here. I completely agree that the problem

    is
    > not with the posted fragment (otherwise, I could generate a testcase).

    The
    > question is what could I possibly be doing wrong prior to the return that
    > would cause an exception when the return is called, while not causing an
    > exception prior to the return.
    >
    >
    >
     
    Victor Hannak, Aug 23, 2003
    #6
  7. Victor Hannak

    White Wolf Guest

    Victor Hannak wrote:
    > Top posting fixed...my apologies...


    Victor, I have only few suggestions to you (since I have never used a system
    where such overflow exception was present).

    RTFM. Take your manual and read when does such overflow exception occur.
    This - even if it will not give you the ahaaaa effect and make you
    immediately find the problem - will at least help you on knowing what are
    you looking for.

    If the previous step did not help to find the error fire up the debugger and
    stop your code right before it would run that very return statement.
    Examine the programs state and look for such things, which can cause the
    overflow (as you have learnt about all of them by reading the manual).

    As these overflow (as far as I know) are not standardized for C++ (I might
    be very wrong about it) you will need to see your docs (compiler or CPU) to
    figure out what can cause such an overflow.

    As a last guess it might also be possible that it is not the return
    statement itself, but you are trying to put the returned number into a type
    (variable?) where it does not fit. But of course it is just a guess, since
    I do not know when can such exception occur. But (for example) if the
    return value gets converted to int (or something else which might overflow)
    you will probably see this as if the return statement itself did throw...

    --
    WW aka Attila
     
    White Wolf, Aug 23, 2003
    #7
  8. Victor Hannak wrote:
    > Top posting fixed...my apologies...
    >


    Um... You are top-posting to tell us that you won't top-post anymore?

    ....


    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 24, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PvdK
    Replies:
    0
    Views:
    2,990
  2. =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=

    Stack overflow exception

    =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,282
    Rick Spiewak
    Apr 22, 2004
  3. wl
    Replies:
    2
    Views:
    594
    Dimitri Maziuk
    Mar 5, 2004
  4. Greenhorn
    Replies:
    15
    Views:
    852
    Keith Thompson
    Mar 6, 2005
  5. stack overflow exception

    , Oct 16, 2006, in forum: C Programming
    Replies:
    5
    Views:
    387
    Peter Shaggy Haywood
    Oct 19, 2006
Loading...

Share This Page