Try Catch within While loop

2k Views Asked by At

I have a SQLite database that I wish to read records from and execute a piece of code against each record.

I am using a While loop with a Try Catch INSIDE it...

Code is as follows:-

            result = slcom.ExecuteReader()
            'TODO: There is a problem with this while loop whereby if an ex is caught the connection
            '      to the database is closed.
            While result.Read
                Try

                       < do some stuff here >

                Catch ex As Exception
                    incrementFailoverCount(result("fid"))
                End Try
            End While
            result.Close()

The problem is, once the Try block is entered and an ex is caught, the next iteration of the while loop fails, as it seems the minute an ex is caught the connection to the SQLite database is closed, eventhough the connection properties state that it is open.

Any ideas ???

1

There are 1 best solutions below

0
On BEST ANSWER

You can move all code within Try Catch into a function that return a boolean and takes a ByRef argument as result of the function

That function return value served as the indicator of the success or failed operation, ie exception being raised or not.