473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

fiddlewidawiddum
Jun 23 '06 #1
2 1994
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
Stimp wrote:
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!
--

fiddlewidawiddum


Jun 23 '06 #2
yes

Karl

--
http://www.openmymind.net/

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
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!
--

fiddlewidawiddum

Jun 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John Bailo | last post by:
how come I can say: if() statement; else statement; but I cannot say try statment; catch() statement;
16
by: Ken | last post by:
What is the purpose of a finally block in try-catch- finally? I am confused because, if I am reading the definition correctly, this block seems unnecessary. Consider the following from the Visual...
6
by: Tilfried Weissenberger | last post by:
Hi, I am a bit confused as to what the FINALLY block is meant for. What's the difference between: this.Cursor = Cursors.WaitCursor; try { //do some stuff } catch { //handle exception }...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
6
by: Alfredo | last post by:
Hi, I have question, i debug this code: try 'Some operations catch Response.Redirect("Page 1") finally Response.Redirect("Page 2")
4
by: James Radke | last post by:
Hello, I am attempting to use the proper Try/Catch technique when accessing my Microsoft SQL server database and have a question... If I use something similar to the following: Try set up...
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
12
by: David Lozzi | last post by:
Howdy, I ran into a very interesting issue and I'm curios as to how this is suppose to work. I am using Try...Catch...Finally statements for all database connectivity in my ASP.NET 2.0 web...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.