473,698 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4934
us****@sta.sams ung.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.sam sung.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.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.sams ung.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.sams ung.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.co mwrote:
use...@sta.sams ung.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 objektorientier ter Datenverarbeitu ng
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.sams ung.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.sams ung.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...@gm ail.comwrote:
On Jun 1, 3:48 am, Ian Collins <ian-n...@hotmail.co mwrote:
use...@sta.sams ung.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 objektorientier ter Datenverarbeitu ng
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

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

Similar topics

10
6714
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 exception for testing purposes? 2. What is the fastest wat to suppress floating point exceptions? Take the following situation as example. float x = 1.0e38, //max float value
7
2337
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 of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log the stack trace and deal with it, normally by displaying an error
12
6105
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 out of which unmanaged exception //get thrown
13
5166
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. From time to time I do something bad -- for example divide by zero or try to grow a float beyond numeric_limits<float>::max() I told MSVC .net 2003 7.1 to "Break into the debugger" for ALL exceptions in
4
1963
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 suite. It seems that I should expect the C code to throw floating point exceptions (SIGFPE) and either the whole thing to abort, or for Python to catch the errors and report them. Instead I'm getting neither. I want to be able to track down the...
2
3471
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
7013
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 type 'long' is used. That way native C speed can be reached. Now I want to experiment with raising a Seed7 exception (which is emulated with setjmp(), longjmp() in C) for integer
0
8672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8890
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8858
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5859
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.