473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Catching floating exceptions

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 '07 #1
24 4896
us****@sta.samsung.com 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.
Jun 1 '07 #2
<us****@sta.samsung.comwrote in message
news:11**********************@p77g2000hsh.googlegr 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;
}

Jun 1 '07 #3
us****@sta.samsung.com 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.
Jun 1 '07 #4
On Jun 1, 5:41 am, use...@sta.samsung.com 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 .

Jun 1 '07 #5
On Jun 1, 3:48 am, Ian Collins <ian-n...@hotmail.comwrote:
use...@sta.samsung.com 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:ja*********@gmail.com
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

Jun 1 '07 #6
terminator wrote:
On Jun 1, 5:41 am, use...@sta.samsung.com 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.

Jun 1 '07 #7
On Jun 1, 9:29 pm, Rolf Magnus <ramag...@t-online.dewrote:
terminator wrote:
On Jun 1, 5:41 am, use...@sta.samsung.com 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

Jun 2 '07 #8
On Jun 2, 2:32 pm, terminator <farid.mehr...@gmail.comwrote:
>
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

Jun 2 '07 #9
On Jun 1, 5:45 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 1, 3:48 am, Ian Collins <ian-n...@hotmail.comwrote:
use...@sta.samsung.com 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:james.ka...@gmail.com
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.

Jun 2 '07 #10
terminator wrote:
On Jun 1, 5:45 pm, James Kanze <james.ka...@gmail.comwrote:
>On Jun 1, 3:48 am, Ian Collins <ian-n...@hotmail.comwrote:
>>use...@sta.samsung.com 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:james.ka...@gmail.com
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.
Jun 2 '07 #11
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)
Jun 2 '07 #12
On Jun 2, 3:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.

Jun 2 '07 #13
terminator wrote:
On Jun 2, 3:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
>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.
Jun 2 '07 #14
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:
terminator wrote:
On Jun 2, 3:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
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: ja*********@gmail.com
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

Jun 3 '07 #15
James Kanze wrote:
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:
>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.
Jun 3 '07 #16
Ian Collins wrote:
James Kanze wrote:
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:
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
Jun 4 '07 #17
Default User wrote:
Ian Collins wrote:
>James Kanze wrote:
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:

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.

Jun 4 '07 #18
Rolf Magnus wrote:
Default User wrote:
Ian Collins wrote:
James Kanze wrote:
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:

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
Jun 4 '07 #19
On Jun 4, 6:38 am, "Default User" <defaultuse...@yahoo.comwrote:
Rolf Magnus wrote:
Default User wrote:
Ian Collins wrote:
>James Kanze wrote:
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:
>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:ja*********@gmail.com
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

Jun 4 '07 #20
James Kanze wrote:
>
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.
James, you forgot to mention the gratuitous munting of your signature!

--
Ian Collins.
Jun 4 '07 #21
On Jun 4, 9:35 am, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
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.
James, you forgot to mention the gratuitous munting of your signature!
Is it still doing it? I think that that was due to the way I'd
integrated vim into Firefox, and (I hope) I've fixed it (on all
of the machines I use).

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Jun 4 '07 #22
James Kanze wrote:
On Jun 4, 6:38 am, "Default User" <defaultuse...@yahoo.comwrote:
Rolf Magnus wrote:
Default User wrote:
Ian Collins wrote:
James Kanze wrote:
On Jun 3, 12:36 am, Ian Collins <ian-n...@hotmail.comwrote:
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.
But that's nothing new. Several of the news services provide web
versions of news readers.

There's no excuse for that - Hide Quoted Text - business. Also no
excuse for not auto-trimming .sigs.

The only real problem I have
with it is that it keeps crashing Firefox.
I haven't had any problem with that, but I haven't used Google to post
much in a couple years.
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).
Oh.


Brian
Jun 4 '07 #23
James Kanze wrote:
On Jun 4, 9:35 am, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
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.
James, you forgot to mention the gratuitous munting of your
signature!

Is it still doing it?
Yep.
I think that that was due to the way I'd
integrated vim into Firefox, and (I hope) I've fixed it (on all
of the machines I use).
Nope.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34

Brian
Jun 4 '07 #24
Thanks to all of you who proposed using setjmp and longjmp, in
conjunction with SIGFPE for handling floating exceptions. It was a
little more involved than I thought due to getting into an infinite
loop, with the signal handler being called endlessly called once the
SIG_FPE exception occurs. I have posted my implementation to
comp.sources.d.

Unfortunately this solution is not fully platform neutral since it
relies on signals.

I am really waiting to see the day when the C++ standard adopts a
consistent way for handling floating point exceptions.

Jun 6 '07 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Vinny | last post by:
I have a few floating point questions/issues I wish to ask. An answer or discussion would be nice too :) Any links discussion the subject would be nice too.. 1. How do I generate an underflow...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
13
by: Chris Stankevitz | last post by:
Hi, I have a very large Visual c++ .net 2003 7.1 native c application (approximately 500,000 lines of code). This application is a simulation that frequently works with floating point numbers....
4
by: John Pye | last post by:
Hi all I have some C code that is giving me some 'nan' values in some calculations. The C code is wrapped using SWIG to give me a Python module that I am then exercising through a unittest...
2
by: fischermx | last post by:
Exception Catching difference between VC++ and C# In C# I have this: try { int x, y, z; x = 20; y = 0;
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.