Jay Marz
Jay Marz

Reputation: 1912

How to catch/handle number format exception in vb.net?

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

Answers (2)

the_lotus
the_lotus

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

Akshay Joy
Akshay Joy

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

Related Questions