473,395 Members | 1,558 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,395 software developers and data experts.

strange behaviour when working with exceptions

Hi,

a strange behaviour when working with exceptions :

when I divide and integer by 0 will an exception be thrown. OK
but, when I divide a double by 0 is no exception thrown ???
How come ?

Try it out.

try
{
int i=1, j=0;
int result = i / j;

// double i=1.0, j=0;
// double result = i / j;

Console.WriteLine("Result {0} / {1} = {2}", i, j,result);
}
catch (Exception pExc)
{
Console.WriteLine("Exception caught : '{0}' ", pExc.Message);
}

can anybody explain this ?

thnx
Chris

Nov 16 '05 #1
2 1310
Hi Chris,

So here what the docs say

"Dividing a floating-point value by zero will result in either positive
infinity, negative infinity, or Not-a-Number (NaN) according to the rules of
IEEE 754 arithmetic. Floating-point operations never throw an exception. For
more information, see Single and Double."

-and-

"If a floating-point operation is invalid, the result of the operation is
NaN."

Why? You probably can find the answer in IEEE specs.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Chris" <ch********@pandora.be> wrote in message
news:uI***********************@phobos.telenet-ops.be...
Hi,

a strange behaviour when working with exceptions :

when I divide and integer by 0 will an exception be thrown. OK
but, when I divide a double by 0 is no exception thrown ???
How come ?

Try it out.

try
{
int i=1, j=0;
int result = i / j;

// double i=1.0, j=0;
// double result = i / j;

Console.WriteLine("Result {0} / {1} = {2}", i, j,result);
}
catch (Exception pExc)
{
Console.WriteLine("Exception caught : '{0}' ", pExc.Message);
}

can anybody explain this ?

thnx
Chris

Nov 16 '05 #2
Chris,

This is right. The double type can represent infinity, positive or
negative, which is what you get when you divide by zero.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <ch********@pandora.be> wrote in message
news:uI***********************@phobos.telenet-ops.be...
Hi,

a strange behaviour when working with exceptions :

when I divide and integer by 0 will an exception be thrown. OK
but, when I divide a double by 0 is no exception thrown ???
How come ?

Try it out.

try
{
int i=1, j=0;
int result = i / j;

// double i=1.0, j=0;
// double result = i / j;

Console.WriteLine("Result {0} / {1} = {2}", i, j,result);
}
catch (Exception pExc)
{
Console.WriteLine("Exception caught : '{0}' ", pExc.Message);
}

can anybody explain this ?

thnx
Chris

Nov 16 '05 #3

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

Similar topics

2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
1
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
2
by: Alfonso Morra | last post by:
Hi, I have written a simple test harness (exe) for a C++ (native) DLL that exports some C++ objects. The objects throw exceptions when they an error occurs in the Ctor, and the test harness is...
2
by: danil.shopyrin | last post by:
Hello! We have some code that is written in COM. It was used in a "ISAPI-extension server " and everithing was fine. And it is fine at the moment. But now this code is used from the ASP.NET...
31
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char...
6
by: Mikus Sleiners | last post by:
Is there any way to enable exception throws in VS 2005, that occur during binding operations? I am upset that i can't see exceptions that are thrown during binding operations. It's very hard to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.