Code for Handling Exception in VB.NET


Here there is a code for a program for handling exception in VB.NET

PROGRAM CODE FOR HANDLING EXCEPTION IN VB.NET


This code shows how to handle exception in vb.net


Public Class Form1
Dim a As Integer
Dim b As Integer
Dim c As Double
Dim d As Integer
Dim a1(4) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try
a = TextBox1.Text
b = TextBox2.Text
c = a / b
TextBox3.Text = c
MsgBox(c)
Catch ex As DivideByZeroException
MsgBox(ex.Message)

End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
a = TextBox1.Text
b = TextBox2.Text
d = a / b
TextBox3.Text = d
MsgBox(c)
Catch ex As OverflowException
MsgBox(ex.Message)
End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
a1(0) = 1
a1(1) = 2
a1(2) = 3
Try
a1(4) = a1(5) + a1(2)
Catch ex As IndexOutOfRangeException
MsgBox(ex.Message)
End Try

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim p As String
Dim q As String
Try
q = p.ToUpper()
Catch ex As NullReferenceException
MsgBox(ex.Message)

End Try
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
a = TextBox1.Text
b = TextBox2.Text
c = a + b
MsgBox(c)
Catch ex As InvalidCastException
MsgBox(ex.Message)
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: