DIVIDE BY ZERO IN VB.NET

DIVIDE BY ZERO IN VB.NET
Dividing by zero is not an error in VB .NET (except for integers). Try the
following:
=====================================================================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Single
Dim y As Single
y = 1 / x 'divide by zero
MsgBox(y)
If [Single].IsInfinity(y) Then
MsgBox("yes, it’s infinity")
End If
End Sub
====================================================================================
IN THE CASE OF INTEGER
====================================================================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Y As Integer
Try
Y = Y / 0
Catch When Y = 0
MessageBox.Show("Y = 0")
End Try
End Sub
=====================================================================================
The first MsgBox will display Infinity. Then the second MsgBox pops up and
says yes, it’s infinity.
Note how you can test for “positive” or “negative” or either kind of infinity
using the IsPositiveInfinity, IsNegativeInfinity, or IsInfinity
methods of the Single or Double type. Also note the odd punctuation. This
is the first time in Visual Basic that brackets have been used in the language

No comments: