Catching floating exceptions

U

usenet

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
 
I

Ian Collins

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.
 
J

Jim Langston

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;
}
 
R

Rolf Magnus

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.
 
T

terminator

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 .
 
J

James Kanze

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.
 
R

Rolf Magnus

terminator said:
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.
 
T

terminator

... on some systems


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
 
T

terminator

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
 
T

terminator

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:[email protected]
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.
 
I

Ian Collins

terminator said:
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?
 
P

Pete Becker

terminator said:
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)
 
J

James Kanze

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.)
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.
 
D

Default User

Ian said:
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
 
R

Rolf Magnus

Default said:
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.
 
D

Default User

Rolf said:
some >> google uses leave in their replies.

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
 
J

James Kanze

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top