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 4 14900
"James Radke" <jr*****@wi.rr. com> wrote in message
news:uT******** ******@TK2MSFTN GP09.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
"James Radke" <jr*****@wi.rr. com> wrote in message
news:uT******** ******@TK2MSFTN GP09.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
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 "immediatel y
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******** ******@TK2MSFTN GP09.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
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******** ******@TK2MSFTN GP09.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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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, it quitted with core dumped.
%test
|
by: Alan Paulk |
last post by:
It seems to me if you have a function like this
public void blah()
try
{
blah blah;
}
catch
|
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) {}
|
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 I can't pre-determine which is which.
Does anyone know how to ensure that the Response.Redirect doesn't cause threading problems?
Thanks,
Tim
|
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 via COM
interops
2 - an exception is thrown because user doesn't exist
3 - Exception is caught by calling code shown below:
| |
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 to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception
way up the stack at a Form.ShowDialog(). I've done a break on...
|
by: dick |
last post by:
in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
|
by: gnuist006 |
last post by:
I have some code like this:
(if (test)
(exit)
(do something))
or
(if (test)
|
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
System.Thread.Timer for each to control their timeout gestion, in my
"main program", I start the "Scenario" and "Step", and I do something
between "StepStart" and "StepStop", when "Scenario" or "Step" timeout
expired, they raise an "exception", then my "main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |