473,796 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Divide By Zero

Neo
Hi Friends,
I am trying following code

int main(void)
{
try
{
int i,j,k;
i=10;
j=0;

k= i/j;
}
catch(...)
{
printf("Erro!~" );
return 1;
}
return 0;
}

Here it's divide by Zero error.

Now using MSVC compiler. the code flow lands up in catch block.

but gcc comiler the code simply throws "floating point exception"
can terminates.

Any comments??

Dec 27 '06 #1
5 7592
Neo wrote:
Hi Friends,
I am trying following code

int main(void)
{
try
{
int i,j,k;
i=10;
j=0;

k= i/j;
}
catch(...)
{
printf("Erro!~" );
return 1;
}
return 0;
}

Here it's divide by Zero error.

Now using MSVC compiler. the code flow lands up in catch block.

but gcc comiler the code simply throws "floating point exception"
can terminates.

Any comments??
The behavior on a division by zero is undefined. So anything can happen. The
exception that gcc mentions is not a C++ exception, but rather a CPU
exception.

Dec 27 '06 #2
Neo

Rolf Magnus wrote:
Neo wrote:
Hi Friends,
I am trying following code

int main(void)
{
try
{
int i,j,k;
i=10;
j=0;

k= i/j;
}
catch(...)
{
printf("Erro!~" );
return 1;
}
return 0;
}

Here it's divide by Zero error.

Now using MSVC compiler. the code flow lands up in catch block.

but gcc comiler the code simply throws "floating point exception"
can terminates.

Any comments??

The behavior on a division by zero is undefined. So anything can happen. The
exception that gcc mentions is not a C++ exception, but rather a CPU
exception.
What you are saying is true. So If I want to write an application which
should be capable ot resuming the execution. What I need to do ? I am
targeting windows,Redhat and solaris platforms.

Dec 27 '06 #3

Neo wrote:
Rolf Magnus wrote:
Neo wrote:
Hi Friends,
I am trying following code
>
int main(void)
{
try
{
int i,j,k;
i=10;
j=0;
>
k= i/j;
}
catch(...)
{
printf("Erro!~" );
return 1;
}
return 0;
}
>
Here it's divide by Zero error.
>
Now using MSVC compiler. the code flow lands up in catch block.
>
but gcc comiler the code simply throws "floating point exception"
can terminates.
>
Any comments??
The behavior on a division by zero is undefined. So anything can happen. The
exception that gcc mentions is not a C++ exception, but rather a CPU
exception.

What you are saying is true. So If I want to write an application which
should be capable ot resuming the execution. What I need to do ? I am
targeting windows,Redhat and solaris platforms.
Check if they are zero before doing the division and throw your own
exception.

Note that if you divide doubles it will not throw an exception if the
divisor is 0.0 but will return a NaN type. (Infinity or Negative
Infinity).

Dec 27 '06 #4
Neo wrote:
>
Rolf Magnus wrote:
>Neo wrote:
Hi Friends,
I am trying following code

int main(void)
{
try
{
int i,j,k;
i=10;
j=0;

k= i/j;
}
catch(...)
{
printf("Erro!~" );
return 1;
}
return 0;
}

Here it's divide by Zero error.

Now using MSVC compiler. the code flow lands up in catch block.

but gcc comiler the code simply throws "floating point exception"
can terminates.

Any comments??

The behavior on a division by zero is undefined. So anything can happen.
The exception that gcc mentions is not a C++ exception, but rather a CPU
exception.

What you are saying is true. So If I want to write an application which
should be capable ot resuming the execution. What I need to do ? I am
targeting windows,Redhat and solaris platforms.
Maybe just use an if-statement to test if 'j' is 0? You can recover in the
if-statement or throw an exception and recover in the catch-block?

alvin
Dec 27 '06 #5
Neo wrote:
Hi Friends,
I am trying following code

int main(void)
{
try
{
int i,j,k;
i=10;
j=0;

k= i/j;
}
catch(...)
{
printf("Erro!~" );
return 1;
}
return 0;
}

Here it's divide by Zero error.

Now using MSVC compiler. the code flow lands up in catch block.

but gcc comiler the code simply throws "floating point exception"
can terminates.

Any comments??
MSVC is fooling you a little bit. It does not throw C++
exceptions, but rather "structured exceptions". Only because
you're using the ellipsis (...) form of catch are you getting
the exception.

Linux and UNIX like systems use "signals" to indicate problems.
You need to create a signal handler. Modern versions of the
gcc compiler allow you to throw an exception from the signal
handler.

/Glen Dayton
Dec 27 '06 #6

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

Similar topics

3
5517
by: Ryan | last post by:
I have the following line in a select statement which comes up with a divide by zero error. CAST(CASE Splinter_Status WHEN 'SUR' THEN 0 ELSE CASE WHEN Sacrifice>=1 THEN 3*m.Premium/100-(m.Sacrifice * 3*m.Premium/100)/ (m.Gross+m.Sacrifice) ELSE 0 END
2
5671
by: Mike Leahy | last post by:
All... I have a query that calculates various using variables from a survey database. As with any survey, there are many instantces of null values. I'm wondering if there is any way to escape the error caused by dividing by zero or null values. The specific message i get is: ERROR: floating point exception! The last floating point operation either exceeded legal ranges or was a divide by zero
0
2102
by: Gary Carson | last post by:
Can anyone tell why the query below would throw a divide-by-zero error? The only reason I can see for the error happening would be if SUM() came out to be zero, but this never happens with the data I'm using. SOME of the values in the EXP_NET column are zero, but the sum itself is always non-zero. The query is: TRANSFORM SUM(-)/SUM()*100 AS DATA SELECT TBL.EQUIPMENT AS EQUIPMENT,
3
2794
by: Joriveek | last post by:
When I specify a formula between Computed Column Specification, I have two zero values, getting Divide by Zero error, any idea how can I avoid this? I still want SQL Server to display Zero if it is 0/0, is this possible in SQL Server database? Thanks J.
4
5150
by: shuisheng | last post by:
Dear all, Assume I have two big arrays A and B. And I want to element-wise divide A by B. When an element of B is zero, the results are also zero. Such as A = { 2, 4, 0, 6} B = { 1, 0, 0, 2} ----------------------------------------- R = { 2, 0, 0, 3}
8
4812
by: =?Utf-8?B?bWljaGFlbGd3ZWllcg==?= | last post by:
Hello! I was working on some code the other day, and I came across an odd discrepancy between the decimal and the double type. If I attempt to divide a decimal by zero, the framework throws an error. If I attempt to divide a double by zero, the framework returns infinity. From a mathematical standpoint, it would seem to me that the decimal handles this division correctly, while the double's handling of this is flawed. Is there a...
9
9102
ollyb303
by: ollyb303 | last post by:
Hi, I have created a report in my Access db which has two columns - I'll call them Number1 and Number2. I have added a textbox to calculate a percentage from these two numbers =(/) with the format property set as percentage. My problem. Sometimes the number in Number2 is a zero, which means the result of the expression is invalid and gives me "#Num!" - I understand why this is happening, but I wondered if there was a way around it....
1
4237
by: zufie | last post by:
Help! How do I divide by zero on my form (in a text box). That is, how do I carry out a division problem when the denominator is zero? Thanks!, John
1
2185
by: Ajay Indian | last post by:
printf("Divide by zero=%d",1/0); is giving output 1 while x=0,y=1; printf("Divide by zero=%d",y/x);
0
9680
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
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10173
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
10006
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...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.