does 'Finally' always run in a try catch statement?

Discussion in 'ASP .Net' started by Stimp, Jun 23, 2006.

  1. Stimp

    Stimp Guest

    I have the following Try, Catch, Finally block...

    Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.
    AppSettings("conn"))
    objConn.Open()

    Try
    Dim strSQL As String = "SELECT COUNT(id) FROM Table1 " & _
    "WHERE idCreatedBy = @idCreatedBy"
    Dim objCmd As SqlCommand = New SqlCommand(strSQL, objConn)

    'Add input parameters
    objCmd.Parameters.Add("@idCreatedBy", SqlDbType.Int).Value = 123

    Return objCmd.ExecuteScalar
    Catch ex As Exception
    Return False
    Finally
    objConn.Close()
    End Try


    my question is: Does the 'Finally' always run even after exiting the
    'Try' statement by using a 'Return' ?


    Thanks!
     
    Stimp, Jun 23, 2006
    #1
    1. Advertisements

  2. Stimp

    V Guest

    Hello,

    yes the finally block still executes.

    Moreover, it will first execute, and then only the function will return
    (to me that was not obvious).

    - Vaibhav
     
    V, Jun 23, 2006
    #2
    1. Advertisements

  3. yes

    Karl
     
    Karl Seguin [MVP], Jun 23, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.