473,395 Members | 1,678 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,395 software developers and data experts.

Try.. Catch... Throw - where does execution stop?

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 SQL connection
open the SQL connection
execute SQL query
Catch
Throw
Finally
close the SQL connection
set the SQL connection object to nothing
end try
Since I am using a Throw to push the error back up to the calling module, if
the execute query causes the issue I am thinking that the throw causes an
immediate stop to the sub - is this correct? so, wouldn't I really want to
also close the connection (after checking if it is open, of course) also
just prior to the throw statement as follows?

Catch ex as exception
if SQL connection open
close SQL connection
end if
set the SQL connection object to nothing
throw ex
Finally ....
Wouldn't this ensure that all resources are freed up/closed correctly?

Every example I have found does not do this, but instead simply closes the
SQL connection in the finally block - am I missing something?

Thanks!

Jim
Nov 20 '05 #1
4 14880

"James Radke" <jr*****@wi.rr.com> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
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 SQL connection
open the SQL connection
execute SQL query
Catch
Throw
Finally
close the SQL connection
set the SQL connection object to nothing
end try

Here's the correct order:

Since I am using a Throw to push the error back up to the calling module, if the execute query causes the issue I am thinking that the throw causes an
immediate stop to the sub - is this correct? so, wouldn't I really want to also close the connection (after checking if it is open, of course) also
just prior to the throw statement as follows?

Catch ex as exception
if SQL connection open
close SQL connection
end if
set the SQL connection object to nothing
throw ex
Finally ....
Wouldn't this ensure that all resources are freed up/closed correctly?

Every example I have found does not do this, but instead simply closes the
SQL connection in the finally block - am I missing something?

Thanks!

Jim

Nov 20 '05 #2

"James Radke" <jr*****@wi.rr.com> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
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 SQL connection
open the SQL connection
execute SQL query
Catch
Throw
Finally
close the SQL connection
set the SQL connection object to nothing
end try


oops

Here's the correct order:

set up SQL connection
open the SQL connection
Try
execute SQL query
Catch
Throw
Finally
close the SQL connection
set the SQL connection object to nothing
end try

The Catch clause is optional, and you can use it to rethrow a more
informative error, perhaps including the SQL query as well as the error
reported from sql server.

Throw does not cause an immediate end to execution. The Finally block
_always_ runs, even after an exception is thrown or a return or other
statement branches execution.
David
Nov 20 '05 #3
James
Try
set up SQL connection
open the SQL connection
execute SQL query
Catch
Throw
Finally
close the SQL connection
end try This is the correct setup.
Every example I have found does not do this, but instead simply closes the
SQL connection in the finally block - am I missing something? You are correct in that a Throw will cause the current sub to "immediately
stop executing", however Finally blocks are always executed! Even with the
Throw in the catch block, the code in the Finally block will be executed.

For details see:
http://msdn.microsoft.com/library/de...ec8_11_1_1.asp

Hope this helps
Jay

"James Radke" <jr*****@wi.rr.com> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl... 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 SQL connection
open the SQL connection
execute SQL query
Catch
Throw
Finally
close the SQL connection
set the SQL connection object to nothing
end try
Since I am using a Throw to push the error back up to the calling module, if the execute query causes the issue I am thinking that the throw causes an
immediate stop to the sub - is this correct? so, wouldn't I really want to also close the connection (after checking if it is open, of course) also
just prior to the throw statement as follows?

Catch ex as exception
if SQL connection open
close SQL connection
end if
set the SQL connection object to nothing
throw ex
Finally ....
Wouldn't this ensure that all resources are freed up/closed correctly?

Every example I have found does not do this, but instead simply closes the
SQL connection in the finally block - am I missing something?

Thanks!

Jim

Nov 20 '05 #4
Looks like David already answered your question, but as a precaution, I'd
recommend wrapping the Open in a try catch block of its own because many
thinga unlrelated to your code and cause that to fail and you can't do
anything with a connection that isn't open.

Cheers,

Bill
"James Radke" <jr*****@wi.rr.com> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
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 SQL connection
open the SQL connection
execute SQL query
Catch
Throw
Finally
close the SQL connection
set the SQL connection object to nothing
end try
Since I am using a Throw to push the error back up to the calling module, if the execute query causes the issue I am thinking that the throw causes an
immediate stop to the sub - is this correct? so, wouldn't I really want to also close the connection (after checking if it is open, of course) also
just prior to the throw statement as follows?

Catch ex as exception
if SQL connection open
close SQL connection
end if
set the SQL connection object to nothing
throw ex
Finally ....
Wouldn't this ensure that all resources are freed up/closed correctly?

Every example I have found does not do this, but instead simply closes the
SQL connection in the finally block - am I missing something?

Thanks!

Jim

Nov 20 '05 #5

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

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
by: Alan Paulk | last post by:
It seems to me if you have a function like this public void blah() try { blah blah; } catch
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
5
by: Tim | last post by:
Hello, I've noticed that sometimes putting a Response.Redirect in a Try-Catch will itself throw an exception due to a threading violation. However, in some cases it works, in others it doesn't, and...
2
by: CodeSlayer | last post by:
Hi all, This one really has me and the other .Net developers at my work stumped. I have an application that is doing the following: 1 - attempt to validate that user can create a windows task...
3
by: Michael C | last post by:
I'm writing an app on the PDA using C# with .net 1.1. It is all working well except in some cases a try catch is simply ignored and a totally different error is returned. I've got code like below...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.