Divide By Zero

N

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

Rolf Magnus

Neo said:
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.
 
N

Neo

Rolf said:
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.
 
E

Earl Purple

Neo said:
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).
 
A

Alvin

Neo said:
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
 
G

Glen Dayton

Neo said:
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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top