Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Catching floating exceptions

Reply
Thread Tools

Catching floating exceptions

 
 
usenet@sta.samsung.com
Guest
Posts: n/a
 
      06-01-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      06-01-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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;
}



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-01-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      06-01-2007
On Jun 1, 5:41 am, (E-Mail Removed) 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 .

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-01-2007
On Jun 1, 3:48 am, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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:(E-Mail Removed)
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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-01-2007
terminator wrote:

> On Jun 1, 5:41 am, (E-Mail Removed) 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.

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      06-02-2007
On Jun 1, 9:29 pm, Rolf Magnus <(E-Mail Removed)> wrote:
> terminator wrote:
> > On Jun 1, 5:41 am, (E-Mail Removed) 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

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      06-02-2007
On Jun 2, 2:32 pm, terminator <(E-Mail Removed)> 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

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      06-02-2007
On Jun 1, 5:45 pm, James Kanze <(E-Mail Removed)> wrote:
> On Jun 1, 3:48 am, Ian Collins <(E-Mail Removed)> wrote:
>
> > (E-Mail Removed) 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:(E-Mail Removed)
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching floating point errors from linked C code John Pye Python 4 01-25-2007 11:53 PM
Catching unhandled exceptions using HttpModules Amil Hanish ASP .Net 0 04-12-2006 11:51 PM
Re: Catching exceptions that are never thrown Adam Maass Java 5 07-22-2003 10:58 PM
Re: Catching exceptions that are never thrown Mike Schilling Java 2 07-16-2003 08:36 PM
Re: catching exceptions from web user controls Marina ASP .Net 2 07-08-2003 04:48 PM



Advertisments