1 Replies - 557 Views - Last Post: 10 September 2010 - 09:42 AM Rate Topic: -----

#1 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Try Catch with a Nonquery

Posted 10 September 2010 - 08:55 AM

Does a finally statement always execute in a try catch or does it only execute if an exception is caught?
I just want to know which is the correct way to execute a nonquery to catch a duplicate entry Access DB error.
                Try
                    myCommand.ExecuteNonQuery()
                Catch ex As Exception
                    rowsAffected = 0
                End Try
                myConnection.Close()


Or

                Try
                    myCommand.ExecuteNonQuery()
                Catch ex As Exception
                    rowsAffected = 0
                Finally
                    myConnection.Close()
                End Try

This post has been edited by eTech1: 10 September 2010 - 08:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Try Catch with a Nonquery

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9055
  • View blog
  • Posts: 33,990
  • Joined: 12-June 08

Re: Try Catch with a Nonquery

Posted 10 September 2010 - 09:42 AM

It always executes - regardless of an exception occurring. Code 2 is right.

Quote

finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited.


http://msdn.microsof...28VS.71%29.aspx

This post has been edited by modi123_1: 10 September 2010 - 09:43 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1