473,748 Members | 10,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

try catch finally to close and dispose, but still wantApplication _error to fire

re: try catch finally to close and dispose, but still want
Application_err or to fire

1. If catch an exception to to dispose and close of a ado connect, how
can I allow the exception to still trigger my application_err or event?

2. And/Or, Is there any way to close and dispose of connections left
behind by methods that might have failed without closing connections
in the application_err or event?
Nov 16 '07 #1
5 2547
Sorry,

I'm not sure if I have missed something from the picture, but I would
say... try --catch --throw

Inside catch if you throw the exception it will be bubbled up again.

HTH
Braulio
--
/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------


"jobs" wrote:
re: try catch finally to close and dispose, but still want
Application_err or to fire

1. If catch an exception to to dispose and close of a ado connect, how
can I allow the exception to still trigger my application_err or event?

2. And/Or, Is there any way to close and dispose of connections left
behind by methods that might have failed without closing connections
in the application_err or event?
Nov 16 '07 #2
use try/finally without a catch, or c# using statement.

-- bruce (sqlwork.com)
jobs wrote:
re: try catch finally to close and dispose, but still want
Application_err or to fire

1. If catch an exception to to dispose and close of a ado connect, how
can I allow the exception to still trigger my application_err or event?

2. And/Or, Is there any way to close and dispose of connections left
behind by methods that might have failed without closing connections
in the application_err or event?
Nov 16 '07 #3
"jobs" <jo**@webdos.co mwrote in message
news:0b******** *************** ***********@f13 g2000hsa.google groups.com...
re: try catch finally to close and dispose, but still want
Application_err or to fire

1. If catch an exception to to dispose and close of a ado connect, how
can I allow the exception to still trigger my application_err or event?

2. And/Or, Is there any way to close and dispose of connections left
behind by methods that might have failed without closing connections
in the application_err or event?
Use finally to close/dispose of connections. The Finally block always
executes.
If your purpose in creating a catch block is to simply close/dispose of
stuff before re-throwing the error then simply don't bother with the catch
block at all. The whole point of the finally block is to perform tidy up
code that must occur whether or not an exception is being thrown.

--
Anthony Jones - MVP ASP/ASP.NET
Nov 16 '07 #4
Use finally to close/dispose of connections. The Finally block always
executes.
If your purpose in creating a catch block is to simply close/dispose of
stuff before re-throwing the error then simply don't bother with the catch
block at all. The whole point of the finally block is to perform tidy up
code that must occur whether or not an exception is being thrown.
--
Anthony Jones - MVP ASP/ASP.NET
Thanks, But I do still need to make sure I close/dispose when an error
occurs. Can I do that in Appliation_erro r sub of the Global.asax? If
so, how? If not I will have to rethrow the same exception? how?

Nov 16 '07 #5
"jobs" <jo**@webdos.co mwrote in message
news:bf******** *************** ***********@a28 g2000hsc.google groups.com...
>
Use finally to close/dispose of connections. The Finally block always
executes.
If your purpose in creating a catch block is to simply close/dispose of
stuff before re-throwing the error then simply don't bother with the
catch
block at all. The whole point of the finally block is to perform tidy
up
code that must occur whether or not an exception is being thrown.
--
Anthony Jones - MVP ASP/ASP.NET

Thanks, But I do still need to make sure I close/dispose when an error
occurs. Can I do that in Appliation_erro r sub of the Global.asax? If
so, how? If not I will have to rethrow the same exception? how?
Let me emphasis what I've already said:-

"The Finally block always executes."

That means even if the flow of code is now in an error condition the code in
the finally block will still execute. Thats why its there. If code in the
finally block didn't execute when an error occured it wouldn't have any
point in being there since it would be no different than putting code at the
bottom of the try block.

So its in the finally block that you would place close/dispose code.

C# provides the using() alternative which allows you to define a block of
code that uses a disposable object that must always be disposed when
execution leaves block. The manner of leaving the block is immaterial,
either by normal code execution or due to an exception.
--
Anthony Jones - MVP ASP/ASP.NET


Nov 17 '07 #6

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

Similar topics

4
3130
by: Big D | last post by:
Hi all, I'm trying to understand the best way to use a try catch with a SQL transaction. I have a number of sql statements that need to be run, such as: 'open sqlConnection, get command object, etc. myTrans.BeginTransaction() 'do a delete 'do a update
23
3081
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
4
3644
by: Cameron Frasnelly | last post by:
The second to last lin of code in the below sub is causing VS2003 to tell me that "Name 'SqlDR' is not declared" when obviously it is. It's also used just fine in the main code... any ideas???? *****CODE BELOW**** Public Sub loadWOFromKEY() Try
13
1582
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType, Description from JnlType", mySqlConnection) Dim Ds As New DataSet("X")
26
2559
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
32
6124
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 understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
28
3841
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " Is there a way around this error? <code> private void Test(string sFileName) {
5
2318
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try Dim cmd As New SqlCommand cmd.CommandText = "spSetAdLinks" cmd.CommandType = Data.CommandType.StoredProcedure cmd.Connection = sqlCon
12
2686
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 application. I'm connecting to IBM's Universe 10.2 using UniObjects.Net. Anyway, if the connection errors, the Finally closes the connection. What I see happening is that the function in the Finally statement either isn't running or doesn't apply what...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9321
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,...
0
9247
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 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...
0
8242
agi2029
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...
1
6796
isladogs
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...
0
4602
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...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
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...

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.