VB.net Try Catch. Keep the Loop after the Catch

徘徊边缘 提交于 2019-12-12 02:56:28

问题


I'm using Try Catch and I want to add into Form3.ListBoxes the items that are into Form2.ListBoxes in other type. But it stop adding after the Catch the exception. So I want to keep the Loop after the exception be caught!

My program get products and show the same products but in another type (Like: I have a T-shirt with a brand, but I want the "same" T-shirt in another brand).

ListBox5 are the quantity that I add in Form1. I load Images to be clearly. Form2 Listboxes are in order (ListBox1,ListBox2...). Form2 and Form3 have the same design.

Dim ConnectionString As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\Tabela_Precos.xlsx; Extended Properties=Excel 12.0;")
        ConnectionString.Open()

        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da

For i = 0 To Form1.ListBox1.Items.Count - 1

        Dim str As String = Form1.ListBox1.Items(i).ToString
        Dim prod As String = str.Substring(2, 3)
        da = New OleDbDataAdapter("SELECT * FROM [Mesa$] WHERE Format([Ref], ""000000000"") like '%" & prod & "%'", ConnectionString)

        da.Fill(dt)
        Try
            ListBox1.Items.Add(dt.Rows(i).Item(0))
            ListBox2.Items.Add(dt.Rows(i).Item(1))
            ListBox3.Items.Add(dt.Rows(i).Item(3))
            ListBox4.Items.Add(dt.Rows(i).Item(5))
            ListBox5.Items.Add(Form1.ListBox5.Items(i))
            ListBox6.Items.Add(ListBox4.Items(i) * ListBox5.Items(i))
        Catch ex As Exception
        End Try
    Next

I need the Try-Catch. I'm doing a Query and if doesn't exist the line in DataBase it stop. How can I keep doing the Loop, after stop?

These are the images with program running (they are edited):

Form1

Form2

Form3


回答1:


You have answered the question yourself when you say "keep the loop after the catch". You want your loop to continue, resume or move to the next iteration. All of these are VB keywords and I recommend you review For Next Statement to understand how a For loop works.

Here is one of a few ways you can accomplish this.

With no error handling, just ignore and continue:

Try
    ...
Catch ex As Exception
    Continue For
End Try


来源:https://stackoverflow.com/questions/35840187/vb-net-try-catch-keep-the-loop-after-the-catch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!