Feeling really dumb...divide by 0

  • Thread starter Thread starter J L
  • Start date Start date
J

J L

I was just playing around with error handling and put this code on a
button

Dim sng1 As Single

Try
sng1 = 1/0
MessageBox.Show(sng1.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message, ex.TargetSite.Name
End Try

To my dismay, the divide by zero doe not throw an exception and the
message box displays the word Infinity...why is this? I feel really
dumb right about now...but what can I say...

TIA
John
 
J L said:
I was just playing around with error handling and put this code on a
button

Dim sng1 As Single

Try
sng1 = 1/0
MessageBox.Show(sng1.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message, ex.TargetSite.Name
End Try

To my dismay, the divide by zero doe not throw an exception and the
message box displays the word Infinity...why is this? I feel really
dumb right about now...but what can I say...

Use 'If Single.IsInfinity(sng1) Then...' to check the result.
 
I can do that but I dont understand why it did not throw an
exception...isn't dividing by 0 an error that should throw an
exception?

Thanks,
John
 
This may give you the info you're after:
http://visualbasic.about.com/od/usingvbnet/l/bldyknaninfa.htm

J L wrote on 3/14/2005 :
I can do that but I dont understand why it did not throw an
exception...isn't dividing by 0 an error that should throw an
exception?

Thanks,
John

--
--------------------------------------------------
Christopher C. Bernholt
I.T. Research & Development
..NET Re-Engineering Team
R & L Carriers, Inc.
http://www.gorlc.com
--------------------------------------------------
 
Hi J L,

From the documentations

"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."
 
Hi Christopher,
Thank you. That explains it. Seems strainge and counter-productive to
me but that is the way it is.

John
 
Back
Top