Hello everyone,
I have verified that the result of 100/0 will not occur any exception, and it will be infinite.
Does it mean there is no exceptions like divide by zero in C#?
thanks in advance,
George
Hello everyone,
I have verified that the result of 100/0 will not occur any exception, and it will be infinite.
Does it mean there is no exceptions like divide by zero in C#?
thanks in advance,
George
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Apr 24, 2008, 2:36 AM EDT
On my machine, the following line produces a 'divide by constant zero' exception:
int i = 100/0;
However, the following line doesn't produce an exception and gives 'd ' a value of (positive) infinity:
double d = 100.0/0.0;
This is because, unlike floating point arithmetic, integer arithmeric has no special values to deal with division by zero. However, the exception is catchable.
George GeorgeRank #1,736 · 870 reputationPosted Apr 24, 2008, 3:13 AM EDT
Thanks Alan,
I have made some further test that for integer/decimal types, there will be divide by zero exception, but for float/double type, there will not by exception. Seems it is in Language Spec,
http://www.csharpfriends.com/Spec/index.aspx?specID=14.7.2.htm
Do you agree with the conclusion? Any further comments? :-)
regards,
George
AlanRank #669 · 3.6K reputationPosted Apr 24, 2008, 5:33 AM EDT
George GeorgeRank #1,736 · 870 reputationPosted Apr 24, 2008, 5:48 AM EDT
Thanks Alan,
Question answered.
regards,
George