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.

    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
    End Try

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

    Stimp, Jun 23, 2006
    1. Advertisements

  2. Stimp

    V Guest


    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
    1. Advertisements

  3. yes

    Karl Seguin [MVP], Jun 23, 2006
    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.