By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,436 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,436 IT Pros & Developers. It's quick & easy.

Divide By Zero

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.