Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm not familiar with exception handling in Visual Basic. Below I have a code snippet with my for-loop. My goal is to throw an exception if the user inputs a string instead of an integer. I would greatly appreciate if someone could show me the correct try catch statement to accomplish this.

VB
For rowindex = 1 To myDataGridView.RowCount
            For columnindex = 1 To myDataGridView.ColumnCount
                    objWorkSheet.Cells(rowindex + 14, columnindex + 0) = myDataGridView(columnindex - 1, rowindex - 1).Value
            Next
        Next
Posted
Comments
ZurdoDev 3-Jun-13 12:00pm    
Inputs a string where?
CAS1224 3-Jun-13 12:09pm    
My program lets the user write data into a datagridview and then reads the data and writes it to an excel file.
ZurdoDev 3-Jun-13 12:11pm    
OK, so as you are reading it, just check for what you need.
CAS1224 3-Jun-13 12:46pm    
Do you know which exception in visual basic can check whether or not the input is an integer or string?
ZurdoDev 3-Jun-13 12:51pm    
Is this VB or VB.Net? It's been a long time since I've used VB but I believe there is an IsNumeric function in VB and possible a few other "Is" functions. However, you could easily test it and see what the exception is. You are better off testing for the value than just catching exceptions.

Hi,
you need something like this,

For example:
VB
Dim inputs As String() = {"1", "2", "3", "tr", "6", "tt", "500", "343", "AA"}
      Dim invalidinput As New List(Of String)
      Dim validinput As New List(Of String)
      For index = 0 To 10
          If (IsvalidNumber(inputs(index)) = True) Then
              invalidinput.Add(inputs(index))
          Else
              validinput.Add(inputs(index))
          End If
      Next

      If (invalidinput.Count > 0) Then
          ' do your stuff
      End If
      If (validinput.Count > 0) Then
          ' do your stuff
      End If


User defined function:

VB
    Public Function IsvalidNumber(ByVal value As Integer) As Boolean
    Dim flag As Boolean = False
    Dim number As Integer
    Try
        number = Convert.ToInt32(value)
        flag = True
    Catch ex As Exception
        ' Do nothing
    End Try
    Return flag
End Function


Regards and thanks
Sarva
 
Share this answer
 
v2
Please, please please do not throw exceptions for incorrect user input. Unless you are writing a DLL or something else that does not have any way of interacting with the user, throwing an exception is the wrong thing to do. You should validate the user input and if its wrong, tell the user and ask them to correct it.

Incorrectly handled exceptions cause application crashes, and exceptions are not meant to be used this way. Just use a message box and stop processing until the error is fixed.
 
Share this answer
 
Comments
CAS1224 3-Jun-13 13:01pm    
How do you make the program stop processing until the error is fixed without using a try catch statement?
Ron Beyer 3-Jun-13 13:10pm    
Validate the input before going into the loop, or use another loop to get valid input (keep asking the user until they give correct input), or exit the loop, etc. There are a lot of ways to do it, but the point is that an exception should never be used for program flow.
CAS1224 3-Jun-13 15:16pm    
Could you give an example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900