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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
George GeorgePosted Apr 24, 2008, 9:48 AM
Thanks Alan,
Question answered.
regards,
George
AlanPosted Apr 24, 2008, 9:33 AM
George GeorgePosted Apr 24, 2008, 7:13 AM
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
AlanPosted Apr 24, 2008, 6:36 AM
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.