Catching floating exceptions

Discussion in 'C++' started by usenet@sta.samsung.com, Jun 1, 2007.

  1. Guest

    I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    using the standard exceptions defined in stdexcept. What is the
    recommended way to catch such exceptions?

    Thanks,
    Song
     
    , Jun 1, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > using the standard exceptions defined in stdexcept. What is the
    > recommended way to catch such exceptions?
    >

    You can't. At least not in standard C++, there might be platform
    specific extensions, but hardware generated traps are not the same as
    C++ language exceptions.

    --
    Ian Collins.
     
    Ian Collins, Jun 1, 2007
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    >I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > using the standard exceptions defined in stdexcept. What is the
    > recommended way to catch such exceptions?


    Is the problem that no exception is thrown? An integer divide by zero will
    throw a system error, a floating point divide by error should return
    infinity or negative infinity. Consider the following program I just threw
    together. There is probably a better way, but I'm just not sure what it is.

    #include <iostream>
    #include <cfloat>

    int main()
    {

    float Zero = 0.0f;
    float Inf = 5.0f / Zero;
    float NegInf = -5.0f / Zero;

    float x = -5.0f;
    float y = 0.0f;
    float z = x/y;

    std::cout << z << "\n";
    if ( z == Inf || z == -Inf )
    std::cout << "Division By Zero (infinity) " << "\n";

    return 0;
    }
     
    Jim Langston, Jun 1, 2007
    #3
  4. Rolf Magnus Guest

    wrote:

    > I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > using the standard exceptions defined in stdexcept. What is the
    > recommended way to catch such exceptions?


    The first thing you should do is decide whether you are programming in C++
    or in D.
     
    Rolf Magnus, Jun 1, 2007
    #4
  5. terminator Guest

    On Jun 1, 5:41 am, wrote:
    > I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > using the standard exceptions defined in stdexcept. What is the
    > recommended way to catch such exceptions?
    >
    > Thanks,
    > Song


    using C (not C++) libraries you can do this via 'signals' and you need
    use 'setjmp' and 'longjmp' functions which are not commonly used since
    the new exception handling mechanism(try-catch-throw) has been
    introduced. I am not certain on details but you need read
    documentations on 'signal' mechanism .
     
    terminator, Jun 1, 2007
    #5
  6. James Kanze Guest

    On Jun 1, 3:48 am, Ian Collins <> wrote:
    > wrote:
    > > I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > > using the standard exceptions defined in stdexcept. What is the
    > > recommended way to catch such exceptions?


    > You can't. At least not in standard C++, there might be platform
    > specific extensions, but hardware generated traps are not the same as
    > C++ language exceptions.


    In general (on a machine with IEEE floating point), divide by 0
    will not generate an exception. Nor a hardware trap, nor
    anything else that will interrupt normal program flow. On most
    systems, anything floating point error which will cause a
    hardware trap will be mapped to SIGFPE, which you can trap, but
    you can't throw an exception from a signal handler, so that may
    not solve the problem either.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 1, 2007
    #6
  7. Rolf Magnus Guest

    terminator wrote:

    > On Jun 1, 5:41 am, wrote:
    >> I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    >> using the standard exceptions defined in stdexcept. What is the
    >> recommended way to catch such exceptions?
    >>
    >> Thanks,
    >> Song

    >
    > using C (not C++) libraries you can do this via 'signals'



    .... on some systems

    > and you need use 'setjmp' and 'longjmp' functions which are not commonly
    > used since the new exception handling mechanism(try-catch-throw) has been
    > introduced.


    There are compilers that actually implement try/catch/throw by means of
    setjmp and longjmp.
     
    Rolf Magnus, Jun 1, 2007
    #7
  8. terminator Guest

    On Jun 1, 9:29 pm, Rolf Magnus <> wrote:
    > terminator wrote:
    > > On Jun 1, 5:41 am, wrote:
    > >> I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > >> using the standard exceptions defined in stdexcept. What is the
    > >> recommended way to catch such exceptions?

    >
    > >> Thanks,
    > >> Song

    >
    > > using C (not C++) libraries you can do this via 'signals'

    >
    > ... on some systems
    >
    > > and you need use 'setjmp' and 'longjmp' functions which are not commonly
    > > used since the new exception handling mechanism(try-catch-throw) has been
    > > introduced.

    >
    > There are compilers that actually implement try/catch/throw by means of
    > setjmp and longjmp.


    but I do not think it is possible to change the way try/catch/throw
    without switching to another compiler
     
    terminator, Jun 2, 2007
    #8
  9. terminator Guest

    On Jun 2, 2:32 pm, terminator <> wrote:
    >
    > but I do not think it is possible to change the way try/catch/throw
    > without switching to another compiler- Hide quoted text -
    >
    > - Show quoted text -


    Sorry.but I do not think it is possible to change the way try/catch/
    throw behaves without switching to another compiler.

    regards,
    FM
     
    terminator, Jun 2, 2007
    #9
  10. terminator Guest

    On Jun 1, 5:45 pm, James Kanze <> wrote:
    > On Jun 1, 3:48 am, Ian Collins <> wrote:
    >
    > > wrote:
    > > > I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    > > > using the standard exceptions defined in stdexcept. What is the
    > > > recommended way to catch such exceptions?

    > > You can't. At least not in standard C++, there might be platform
    > > specific extensions, but hardware generated traps are not the same as
    > > C++ language exceptions.

    >
    > In general (on a machine with IEEE floating point), divide by 0
    > will not generate an exception. Nor a hardware trap, nor
    > anything else that will interrupt normal program flow. On most
    > systems, anything floating point error which will cause a
    > hardware trap will be mapped to SIGFPE, which you can trap, but
    > you can't throw an exception from a signal handler, so that may
    > not solve the problem either.
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    wont it be good to provide a standard for libraries so as to declare
    functions that convert old style exceptions to new ones?
    currently ,static variable constructors also may throw exceptions that
    I am not sure could be catched.
     
    terminator, Jun 2, 2007
    #10
  11. Ian Collins Guest

    terminator wrote:
    > On Jun 1, 5:45 pm, James Kanze <> wrote:
    >> On Jun 1, 3:48 am, Ian Collins <> wrote:
    >>
    >>> wrote:
    >>>> I am unable to catch floating exceptions (e.g. divide by 0 or 0/0)
    >>>> using the standard exceptions defined in stdexcept. What is the
    >>>> recommended way to catch such exceptions?
    >>> You can't. At least not in standard C++, there might be platform
    >>> specific extensions, but hardware generated traps are not the same as
    >>> C++ language exceptions.

    >> In general (on a machine with IEEE floating point), divide by 0
    >> will not generate an exception. Nor a hardware trap, nor
    >> anything else that will interrupt normal program flow. On most
    >> systems, anything floating point error which will cause a
    >> hardware trap will be mapped to SIGFPE, which you can trap, but
    >> you can't throw an exception from a signal handler, so that may
    >> not solve the problem either.
    >>
    >> --
    >> James Kanze (GABI Software) email:
    >> Conseils en informatique orientée objet/
    >> Beratung in objektorientierter Datenverarbeitung
    >> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    >
    > wont it be good to provide a standard for libraries so as to declare
    > functions that convert old style exceptions to new ones?


    Signals are not "old style exceptions". They are asynchronous events.

    > currently ,static variable constructors also may throw exceptions that
    > I am not sure could be catched.
    >

    Caught by what?

    --
    Ian Collins.
     
    Ian Collins, Jun 2, 2007
    #11
  12. Pete Becker Guest

    terminator wrote:
    > currently ,static variable constructors also may throw exceptions that
    > I am not sure could be catched.
    >


    They can, through a somewhat obscure mechanism called a "function try
    block." Here's an example from the standard:

    int f(int);
    class C {
    int i;
    double d;
    public:
    C(int, double);
    };

    C::C(int ii, double id)
    try
    : i(f(ii)), d(id)
    {
    // constructor function body
    }
    catch (...)
    {
    // handles exceptions thrown from the ctor-initializer
    // and from the constructor function body
    }


    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Jun 2, 2007
    #12
  13. terminator Guest

    On Jun 2, 3:20 pm, Ian Collins <> wrote:
    > terminator wrote:
    > Signals are not "old style exceptions". They are asynchronous events.
    >


    yes they are traps/interupts(which one?)

    > > currently ,static variable constructors also may throw exceptions that
    > > I am not sure could be catched.

    >
    > Caught by what?
    >
    > --
    > Ian Collins.- Hide quoted text -
    >
    > - Show quoted text -


    Got it.on second thought it seems meaningless.
     
    terminator, Jun 2, 2007
    #13
  14. Ian Collins Guest

    terminator wrote:
    > On Jun 2, 3:20 pm, Ian Collins <> wrote:
    >> terminator wrote:
    >> Signals are not "old style exceptions". They are asynchronous events.
    >>

    >
    > yes they are traps/interupts(which one?)


    I chose my words with care, they are events. How they are delivered is
    a matter for the implementation.
    >
    >>> currently ,static variable constructors also may throw exceptions that
    >>> I am not sure could be catched.

    >> Caught by what?
    >>
    >> --
    >> Ian Collins.- Hide quoted text -
    >>
    >> - Show quoted text -

    >

    Please don't quote signatures or all that google crap.

    --
    Ian Collins.
     
    Ian Collins, Jun 2, 2007
    #14
  15. James Kanze Guest

    On Jun 3, 12:36 am, Ian Collins <> wrote:
    > terminator wrote:
    > > On Jun 2, 3:20 pm, Ian Collins <> wrote:
    > >> terminator wrote:
    > >> Signals are not "old style exceptions". They are asynchronous events.


    > > yes they are traps/interupts(which one?)


    > I chose my words with care, they are events. How they are delivered is
    > a matter for the implementation.


    The important thing is that, regardless of what you call them,
    they asynchronously interrupt the normal program flow, and that
    there are only a very, very limited number of things you can do
    in a signal handler. (Throwing an exception is not one of
    them.)

    > >>> currently ,static variable constructors also may throw exceptions that
    > >>> I am not sure could be catched.
    > >> Caught by what?


    > >> --
    > >> Ian Collins.- Hide quoted text -


    > >> - Show quoted text -


    > Please don't quote signatures or all that google crap.


    Don't blame it on Google. I post through Google, and I don't
    quote signatures. It's true that Google could strip the
    signatures in the Post box, but it couldn't reasonably strip
    anything else that is irrelevant---so you'd still have to trim
    by hand.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 3, 2007
    #15
  16. Ian Collins Guest

    James Kanze wrote:
    > On Jun 3, 12:36 am, Ian Collins <> wrote:
    >
    >> Please don't quote signatures or all that google crap.

    >
    > Don't blame it on Google.


    I wasn't, I was whinging about the - Hide quoted text - nonsense some
    google uses leave in their replies.

    --
    Ian Collins.
     
    Ian Collins, Jun 3, 2007
    #16
  17. Default User Guest

    Ian Collins wrote:

    > James Kanze wrote:
    > > On Jun 3, 12:36 am, Ian Collins <> wrote:
    > >
    > >> Please don't quote signatures or all that google crap.

    > >
    > > Don't blame it on Google.

    >
    > I wasn't, I was whinging about the - Hide quoted text - nonsense some
    > google uses leave in their replies.


    Which is also Google's fault.



    Brian
     
    Default User, Jun 4, 2007
    #17
  18. Rolf Magnus Guest

    Default User wrote:

    > Ian Collins wrote:
    >
    >> James Kanze wrote:
    >> > On Jun 3, 12:36 am, Ian Collins <> wrote:
    >> >
    >> >> Please don't quote signatures or all that google crap.
    >> >
    >> > Don't blame it on Google.

    >>
    >> I wasn't, I was whinging about the - Hide quoted text - nonsense some
    >> google uses leave in their replies.

    >
    > Which is also Google's fault.


    Just like about all the spam you find in newsgroups. It's almost always
    posted via Google groups, because their disposable accounts make abuse
    reports worthless.
     
    Rolf Magnus, Jun 4, 2007
    #18
  19. Default User Guest

    Rolf Magnus wrote:

    > Default User wrote:
    >
    > > Ian Collins wrote:
    > >
    > >> James Kanze wrote:
    > >> > On Jun 3, 12:36 am, Ian Collins <> wrote:
    > >> >
    > >> >> Please don't quote signatures or all that google crap.
    > >> >
    > >> > Don't blame it on Google.
    > >>
    > >> I wasn't, I was whinging about the - Hide quoted text - nonsense

    > some >> google uses leave in their replies.
    > >
    > > Which is also Google's fault.

    >
    > Just like about all the spam you find in newsgroups. It's almost
    > always posted via Google groups, because their disposable accounts
    > make abuse reports worthless.


    That's true, but a different problem. There's no excuse whatsoever for
    their news interface to work as poorly as it does. It's not like
    they're exactly breaking new ground here.




    Brian
     
    Default User, Jun 4, 2007
    #19
  20. James Kanze Guest

    On Jun 4, 6:38 am, "Default User" <> wrote:
    > Rolf Magnus wrote:
    > > Default User wrote:
    > > > Ian Collins wrote:
    > > >> James Kanze wrote:
    > > >> > On Jun 3, 12:36 am, Ian Collins <> wrote:


    > > >> >> Please don't quote signatures or all that google crap.
    > > >> > Don't blame it on Google.
    > > >> I wasn't, I was whinging about the - Hide quoted text -
    > > >> nonsense some google uses leave in their replies.


    > > > Which is also Google's fault.


    > > Just like about all the spam you find in newsgroups. It's almost
    > > always posted via Google groups, because their disposable accounts
    > > make abuse reports worthless.


    > That's true, but a different problem. There's no excuse
    > whatsoever for their news interface to work as poorly as it
    > does. It's not like they're exactly breaking new ground here.


    They do have to make it work within a browser, which is not
    designed for this sort of thing. The only real problem I have
    with it is that it keeps crashing Firefox. This is obviously a
    bug in Firefox (a program shouldn't crash, no matter what it is
    sent), but the problem only occurs when I'll reading news
    through Google (and then only when I'm running it with a remote
    X server---for various reasons, the only way I can access news
    at this site is through Google, using Firefox running on a Linux
    box, addressing an X server on a Sparc under Solaris).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 4, 2007
    #20
    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. Marina
    Replies:
    2
    Views:
    478
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    552
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    407
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    353
    Mike Schilling
    Jul 16, 2003
  5. John Pye
    Replies:
    4
    Views:
    378
    John Pye
    Jan 25, 2007
Loading...

Share This Page