Reputation: 1912
I would like to add 'try catch' statement on the module that I'm creating. I want to specify the exception to be caught in number format. This is the sample code,
Try
interest = me.txtInterest.text
principal = me.txtPrincipal.text
totalPayment = interest + principal
Catch ex As Exception 'What is the proper exception for Number Format?
MsgBox("Number Format Error")
End Try
I want to specify the exception to number format. How could I do that?
Upvotes: 1
Views: 3981
Reputation: 12748
Instead of catching the exception, you could check if it's a valid number
If Int32.TryParse(me.txtInterest.text, interest) AndAlso Int32.TryParse(me.txtPrincipal.text, principal) Then
totalPayment = interest + principal
Else
MsgBox("Number Format Error")
End If
Upvotes: 0
Reputation: 1765
use this sample code
Try
Dim no1 As Integer = Int16.Parse(Me.TextBox1.Text)
Dim no2 As Integer = Int16.Parse(Me.TextBox2.Text)
Dim toatlPayment As Integer = no1 + no2
Catch ex As FormatException
MessageBox.Show(ex.Message)
End Try
Upvotes: 2