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

S

Stimp

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!
 
V

V

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top